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

FULL DISCUSSION
 

I broke out the work a little more, I tried to be a tad clever and utilize switch but wasn't able to get it to work quite the way I wanted.

Hopefully, the slightly more vanilla JS will be useful in some way.

function XO(s) {
  const arr = [...s];
  let countX = 0;
  let countO = 0;
  arr.map((ele, i) => {
    if (ele === `x`) {
      countX++;
      delete arr[i];
    }
    if (ele === `X`) {
      countX++;
      delete arr[i];
    }
    if (ele === `o`) {
      countO++;
      delete arr[i];
    }
    if (ele === `O`) {
      countO++;
      delete arr[i];
    } else {
      delete arr[i];
    }
  });
  const compare = countO === countX;
  return compare;
}

console.log(XO(`xxxoooxxxoooxxxoooxoxoppppppppppppxoooxx`)); // true
// console.log(XO(`ooxx`)); // true
// console.log(XO(`xooxx`)); // false
// console.log(XO(`zpzpzpp`)); // true
// console.log(XO(`zzoo`)); // false
// console.log(XO(`zzoOOOoo`));
 

I was removing elements with plans of creating two for loops going in opposite directions to speed up the process... but that was for fun and it lost interest in it.

code of conduct - report abuse