Tower Breakers, Revisited!

  • + 0 comments

    JS:

    function towerBreakers(arr) {
        // Write your code here
       let sumArr = [];
      for (let i = 0; i < arr.length; i++) {
        let sum = 0;
        if (arr[i] === 2 || arr[i] === 3) sum++;
        else {
          for (let j = 2; j <= arr[i]; j++) {
            while (arr[i] % j == 0) {
              sum++;
              arr[i] = arr[i] / j;
            }
          }
        }
        sumArr.push(sum);
      }
      return sumArr.reduce((a, v) => (a ^= v)) ? "1" : "2";
    }