re: Daily Challenge #29 - Xs and Os VIEW POST

FULL DISCUSSION
 

How about a reduce to keep it in one loop? (JS)

XO = str => 
  [...str].reduce(
    (acc, c) => acc + (c == 'x' || c == 'X') - (c == 'o' || c == 'O'),
    0
  ) == 0;

// Check it
["ooxx","xooxx","ooxXm","zpzpzpp","zzoo"].map(s =>
  `${s} => ${XO(s)}`
).join('\n');
/* ^
"ooxx => true
xooxx => false
ooxXm => true
zpzpzpp => true
zzoo => false"
*/
 

Oh, nice! I wouldn't have thought to use the array spread syntax to get the array of characters. 🎩✨

code of conduct - report abuse