beginner..TicTacToe logic issue

 (col === oTarget ? oCount++ : oCount = 0 || col === xTarget ? xCount++ : xCount = 0) 

so this is part of a horizontal win check method in my tic tac toe object.
oTarget = ‘o’
xTarget = ‘x’
oCount = 0;
xCount = 0;

just on a logic level why wouldn’t the ternaries being false address situations in which a sparse index is encountered on a 2D board and reset the count to 0?

0 is a falsy value.

let falsy;
const test = false ? 'Condition 1' : falsy = 0 || true ? 'Condition 2' : 'Whatever';

console.log(test); // Condition 2
console.log(falsy); // Condition 2

As an aside. That really isn’t how you want to use ternaries. They are expressions that should evaluate to a value, not perform side effects. You usually have a single return or assignment at the start.

I would also avoid nesting them for the most part as they can get hard to read and reason about.

1 Like