Help in tic_tac game in isWin function

Help in tic_tac game in isWin function
0.0 0

#1

hello
so in building the game i have a function called isWin
to detect if the player win or not
and something strange happened
expression like this not working
if(arr[0]==arr[1]==arr[2]){do something}
in this array i change every index to the char when
player have move
and it store it correctly
i check that with alert(arr);
and if player click first 3 squares on game
arr[0]==arr[1]==arr[2]
but the if block not working
i try in the console
let arr=[“d”,“d”,“d”]
and work why this is not working
in the game


#2

You can not write it like:

if(arr[0]==arr[1]==arr[2]){do something}

Instead, you need to write it like:

if ((arr[0]==arr[1]) && (arr[0]==arr[2])) { do something }

If you had many strings or numbers in an array and you wanted to check to see if all the items were equal, you could use the every function like:

const arr1 = ['d','d','d','d','d','d','d','d'];
arr1.every(elem => arr1[0] === elem); // true

const arr2 = ['d','d','d','d','d','d','e','d'];
arr2.every(elem => arr1[0] === elem); // false