solution
const XO = ([...str]) => {
str = str.map(x => x.toLowerCase());
let counter = [0,0];
if(str.length === 1) return false;
if(!str.includes('0') && !str.includes('x')) return true;
str.forEach( x => x === 'o' ? counter[0]++ : x === 'x' ? counter[1]++ : 0)
return counter[0] === counter[1];
}
Your first 2 conditions are completely unnecessary
why not this? const XO = (str) => { const count = [...str.toLowerCase()] // .filter(l => l == "o" || l == "x") .reduce((obj, l) => { obj[l]++; return obj; }, { x: 0, o: 0 }); return count.x == count.o; }
const test = "OOxx"; const check = str => { const countO = (str.match(/O/gi) || []).length; const countX = (str.match(/X/gi) || []).length; if (countO == countX) { return true; } else { return false; } }; console.log(check(test));
Обсуждают сегодня