Basic JavaScript - Stand in Line

Hello,

My code is the following:

function nextInLine(arr, item) {
  // Only change code below this line
    function nextInLine(arr,item){
    arr.push(item)
    return arr.shift()

  }
  return item;
  // Only change code above this line
}

// Setup
let testArr = [1, 2, 3, 4, 5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

I’m not passing the last test which is the following;

nextInLine([2], 1)

According to freecodecamp, this should return 2. However I don’t understand why because the function nextInLine is supposed to basically push number 1 to the array and remove 2 which is the first value of the array.

Is this test wrong?

Can you post a link to the challenge please?

1 Like

Absolutely! Sorry about that

This is the link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line

I just noticed my error. I was using a callback function, didn’t pay attention to it until now.
The solution is the following:

function nextInLine(arr, item) {
  // Only change code below this line
    arr.push(item)
    return arr.shift()

  return item;
  // Only change code above this line
}

// Setup
let testArr = [1, 2, 3, 4, 5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

Yes, your solution works fine but you only need the first return statement. The other is redundant.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.