.every() prototype isn't working as expected

26 Views Asked by At

Obviously it works for people so I must just be dumb, but I am passing an array of boolean's through the .every() prototype and it ALWAYS appears to return true, even when it shouldnt. so heres the code

The function call is made

checkAnswer();

Then this function executes.

      const checkAnswer = () => {
    let arr = cardStyle.slice(lengthTest - 5, lengthTest);
    if (arr.every((x) => x === true)) {
      Alert.alert("Congratulations!", `You Won in ${rowCount} tries!`);
    }
  };

For a test case, this function uses the React State Variable "cardStyle", which equals [false,false,false,false,false] (Booleans, not strings) and "rowCount" which equals 1. Then there is a global variable lengthTest which is initially equal to 5.

The weird behavior is that no matter what, the condition (arr.every((x) => x === true)) always seems to evaluate to true, because the Alert box always pops up. What am I missing?

0

There are 0 best solutions below