0

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?

  • Have you verified that `arr` has a non-zero length? – Pointy Feb 25 '22 at 19:47
  • [I cannot reproduce it](https://jsbin.com/kicovoq/edit?js,console). Please include a [mcve]. Also consider [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Feb 25 '22 at 19:49
  • FWIW, `[false,false,false,false,false].slice(0, 5).every(x => x === true)` returns `false`. Either `cardStyle` isn't what you think it is or `lengthTest` isn't what you think it is. – ray Feb 25 '22 at 19:49
  • @Pointy so I just checked this. arr has a length of zero. I don't understand why this is though... upon closer inspection, cardStyle might not actually be being updated before checkAnswer is called. That makes no sense though, because the state is updated at the end of the previous function call. I know react bundles state updates, but I would expect that it would at least update by the time its enclosing function ends. – Quinten Steenwyk Feb 25 '22 at 19:53
  • 1
    @QuintenSteenwyk this is not true. Unless you're in a `setState` callback in a class, there is no way to get the updated state until the next render. – Brian Thompson Feb 25 '22 at 19:54
  • @ray you are correct about cardStyle. If you see my other comment you will see the issue. I believe it has to do with react state updates, but I could be entirely mistaken. – Quinten Steenwyk Feb 25 '22 at 19:56
  • In the future, I would very strongly suggest at least checking the actual value of your variables before posting a question. Not only is it essential information for question answerers to have posted, but it would it have told you you're chasing the wrong problem entirely – Brian Thompson Feb 25 '22 at 19:59
  • @BrianThompson I was attempting to find the value before hand, but I struggled since the console output was getting buried amongst all the others. I expected it to print last but it printed before all of the state updates, which is why I was confused. Now I realize that I have to figure out how to execute the function after state updates. – Quinten Steenwyk Feb 25 '22 at 20:09

0 Answers0