Counter game

  • + 0 comments

    Typescript solution

    function counterGame(n: number): string {
        let turn = true
        // Write your code here
        const isPowerOfTwo = (num:number):{isPower: boolean, lowerPower:number} => {
            let calc = 1
            let power = 0
            while(calc < num){
                power += 1
                calc *=2
            }
            if(calc == num){
                return {
                    isPower: true,
                    lowerPower: 0
                }
            } else {
                return {
                    isPower: false,
                    lowerPower: calc/2 
                }
            }
        }
        while(n != 1){
            const isPowtwo = isPowerOfTwo(n)
            if(isPowtwo.isPower){
                n = n/2
            } else {
                n -= isPowtwo.lowerPower
            }
            turn = !turn
        }
    return turn?'Richard': 'Louise';
    
    }