New Year Chaos

  • + 0 comments

    Hi folks, anyone able to suggest why this wouldn't be working? It passes all the sample tests, and tests 0, 1, and 11, but not the others. Have tried taking the test inputs and checking a few parts of them and they seem to work, so I don't know where the issue is.

    It's not failing on time

    function minimumBribes(q) {
        // Write your code here
        let bribes = 0;
        const reference = [...q];
        reference.sort((a, b)=> {
            return a-b
        })
        for(let i=0; i<q.length; i++){
            if(q[i] > reference[i]){
                let difference  = q[i] - (reference[i])
                if(difference > 2){
                    console.log('Too chaotic')
                    return
                } else {
                    bribes += difference
                }
            } else if(q[i] < reference[i] && q[i+1] < q[i]){
                bribes ++
            } else {
                null
            }  
        }
        console.log(bribes)
    }