samedi 5 décembre 2020

How to specify multiple conditions in an array and call it in an if statement in javascript

I don't know this can be possible or not I want to store all my condition in an array and need to call it in if statement

const addition = (...numbers) => {

    let arrayOfTest = [
        `${numbers.length === 0}`,
        `${numbers.some(isNaN)}`,
        `${numbers === null}`,
    ];

    if (arrayOfTest.includes(true)) {
        throw new Error("Invalid Input");
    } else {
        return numbers.reduce((a, b) => {
            return a + b;
        });
    }
};

console.log( addition(1, 3, 4, 5, 7, 8));

Is this possible? Can I write all my conditions in the array list and call it in if statement

Aucun commentaire:

Enregistrer un commentaire