Issue with JavaScript Stay in Line

Tell us what’s happening:
I am attempting to complete this challenge, but the test conditions are impossible to fulfill. The test states that passing in the parameters ([2], 1) should return a value of 2, but I am not understanding how to do that. I watched the video, and my solution matches it but I still cannot complete the assignment.

Your code so far


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


}

// Setup
var 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));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

Welcome to the forum.

The challenge is solvable. The instructions are

Add the number to the end of the array, then remove the first element of the array.

You are adding an item to the beginning of the array instead of the end. You do have the correct code to remove the first element.

This question relies on a fundamental understanding of Queue’s. Queue’s follow a FIFO ordering; FIFO just stands for, “First in First out”. Elements that are added first should always be removed before anything that’s added later.

From the problem we’re told that new items can be added at the back of the queue and old items are taken off from the front of the queue.

Our function nextInLine takes an array ( arr ) and a number ( item ) as arguments.

We need to: Add the number (item given to us as an argument) to the end of the array, then remove the first element of the array.

Now, let’s look at what you were doing:

The first line calls the unshift() method on an array which adds new items to the beginning. Is that what we wanted to do? Recall that, we need to: Add the number (item given to us as an argument) to the end of the array. So, it looks like the method you’re calling here doesn’t have the behavior we want. You’re correctly calling the shift() method. So, what methods could we use to get the desired functionality? Well I’ll refer you to some documentation here. (https://www.w3schools.com/jsref/jsref_unshift.asp

Here’s a correct solution if you get REALLY stuck.

function nextInLine(arr, item) {
arr.push(item)
return arr.shift()

Thanks, I misread the instructions. I appreciate the clarification.