function andMultipleExpr(){
let logicalAnd;
let i;
for (i = 0; i < arguments.length; i++){
logicalAnd = arguments[i] && arguments[i+1];
}
return logicalAnd;
}
console.log(andMultipleExpr(true, true, false, false));
What I am expecting is to execute this code: true&&true&&false&&false and that should return false.
How to make that work in js? Thanks
Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.
Luckily, you can write functions that take in more than one parameter by defining as many parameters as needed, for example: def function_name(data_1, data_2):
Use Array.prototype.every
on all the passed arguments to check if they all are true;
function andMultipleExpr(...a) {
if(a.length === 0) return false; // return false when no argument being passed
return a.every(Boolean);
}
console.log(andMultipleExpr(true, true, false)); // should return false
console.log(andMultipleExpr(true, true, true)); // should return true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With