I'm trying to figure out why, in my reducer function, can isTrue flip to true from false, but not back to false when encountering a function that returns false . I'm just trying to understand why it can flip one way but not the other.
I know the flip happens in return isTrue || val(num);
Directions were "// define a function "passOne" which takes two arguments, the first is any value and the second is an array of functions. These functions can be considered tests, as they will all return either true or false. "passOne" will iterate/loop through the array and pass the value to each function as an argument. If at least one function(test) returns true, "passOne" will return true. If none of the functions return true, "passOne" will return false. // use your "passOne" to determine if the number 113 is negative, even, OR has 1 as the first digit. Then test 213."
i've tried visualizing it in Python Tutor.
function isNeg(num){
return num < 0 ? true: false;
}
function isEven(num){
return num % 2 === 0 ? true : false;
}
function oneDig(num){
return (num.toString())[0] === `1` ? true : false;
}
const funcs = [oneDig, isNeg, isEven];
// refactor "passOne" so that it uses the built-in "reduce" method instead of a "for" loop
function passOne(num, arr){
return arr.reduce((isTrue, val) => {
return isTrue || val(num);
}, false);
}
console.log(passOne(113, funcs)); //true
console.log(passOne(213, funcs)); //false
code is behaving as expected, I'm just not understanding why callbacks that return 'false' don't flip isTrue back to 'false', how does it stay 'true' and then not update to 'false', not that that's what the instructions say to do, i'm just curious.