Gaming Array 1

  • + 0 comments

    JS:

    function gamingArray(arr) {
        const getMaxIdx = (maxIdx)=>{
            let max = 0;
            for (let i = 0; i < maxIdx; i++){
                if (arr[i] > max) max = arr[i];
            }
            return arr.indexOf(max);
        }
        
        let cnt = 0;
        let idx = arr.length;
        while (idx > 0){
            idx = getMaxIdx(idx); 
            cnt++;
        }
        return (cnt%2 === 0) ? 'ANDY' : 'BOB'
    }