Counter game

  • + 0 comments

    Javascript Solution

    function counterGame(n) {
      function checkBase(number) {
        const base = Math.log(number) / Math.log(2);
        if (Number.isInteger(base)) {
          return  number / 2;
        } else {
          const closestBase = Math.floor(base);
          return number - 2 ** closestBase;
        }
      }
    
      let counter = 0;
    
      while (n > 1) {
        n = checkBase(n);
        counter++;
      }
    
      if (counter % 2 === 1) {
        return "Louise";
      } else {
        return "Richard";
      }
    }