Counter game

  • + 0 comments

    Javascript

    function counterGame(n) {
        let turn = 0;
        let log = Math.log2(n);
        while (log % 1 !== 0) {
            n -= Math.pow(2, Math.floor(log)); //subtract nearest power of 2
            turn++;
            log = Math.log2(n);
        }
        const endTurn = turn + log;
        return endTurn % 2 === 0 ? "Richard" : "Louise";
    }