• + 1 comment

    It seems like there is actualy an error in the example. Going from the second step to the third step in the example, the array gets "8,7" removed but "7" is still included in the array after it was already removed. I'm also not sure why my code only passes some tests but not all. I tried testing edge cases but it seems like everything works fine.

    function cookies(k, A) {
        let array = A.sort((a,b) => a-b)
        let counter = 0
        if (array[0] >= k  || array.length <= 1){
            return -1
        } else {
            while (array[0] < k){
                let first = array.shift()
                let second = array.shift()
                let third = parseInt(first) + 2 * parseInt(second)
                array.push(third)
                array.sort((a,b) => a-b)
                counter += 1
            }   
            return counter
        }
    }