DEV Community

Discussion on: Daily Challenge #29 - Xs and Os

Collapse
 
ynndvn profile image
La blatte • Edited

How about some ugly oneliner?

XO=p=>!(new Set(p.toLowerCase().split``.reduce((l,i)=>[l[0]+(i=='x'),l[1]+(i=='o')],[0,0])).size-1)

Which produces the following output:

XO("ooxx"); // true
XO("xooxx"); // false
XO("ooxXm"); // true
XO("zpzpzpp"); // true
XO("zzoo"); // false

It basically works by lowercasing everything and building an array with the number of Xs and Os. After that, it builds a Set with it, and check if the Set size is 1 or 2!