HackerRank: New Year Chaos in JavaScript

This problem is rated as medium difficulty. Had to look up clues in comments section though.

My initial solution was very straight forward:

function minimumBribes(q) {
    let bribes = 0;
    
    for (let i=0; i<q.length-2; i++) {
        if (q[i] - (i+1) > 2) {
            console.log('Too chaotic');
            return;
        }
        // compare index to element, works great except when a smaller element is pushed way back
        if (q[i] - (i+1) > 0) {
          bribes += q[i] - (i+1);
        } 
    }
    console.log(bribes);
}

Second solution had n**2 complexity but still easy to understand

function minimumBribes(q) {
    let bribes = 0;
    
    for (let i=0; i<q.length; i++) {
        if (q[i] - (i+1) > 2) {
            console.log('Too chaotic');
            return;
        }
        // if there is a bigger  element before this element
        // then that element must have bribed this element
        for (let j=0; j<i; j++) {
            if (q[i]<q[j]) {
                bribes++;
            }
        }
    }
    console.log(bribes);
}

Above solution timed out, so finally I looked in discussion section and used copied this solution, since it is O(n) and easy to understand.

function minimumBribes(q) {
    let bribes = 0;
    let expectedFirst = 1;
    let expectedSecond = 2;
    let expectedThird = 3;
    
    for (let i=0; i<q.length-0; i++) {
        if (q[i] == expectedFirst) {
            expectedFirst = expectedSecond;
            expectedSecond = expectedThird;
            ++expectedThird;
        } else if (q[i] == expectedSecond) {
            ++bribes;
            expectedSecond = expectedThird;
            ++expectedThird;
        } else if (q[i] == expectedThird) {
            bribes += 2;
            ++expectedThird;
        } else {
            console.log("Too chaotic");
            return;
        }
    }
    console.log(bribes);
}