Stand in Line: JavaScript

I can’t seem to understand what the challenge wants me to do or fail to meet it:

  **Your code so far**

function nextInLine(arr, item) {
// Only change code below this line
return item;
var removed = 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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

There are a couple of issues with your code, but all of them relatively easy to fix and understand.
Just to reiterate what the challenge wants you to do:
Write a function. That function will receive an array and an item as input.
You should add that item to the end of the array.
You should remove the first item of the array and return it (“it” being the item)

Alright, so let’s go through your code:

  1. The first issue is, that you are pushing 6 to the end of the array. However you should push item to the array. You get item from the input.

  2. You are not removing anything from the array within your function. You are also getting an error because of this, because arr is only defined within the function, since it is given as input to the function.

  3. You are returning item, as opposed to the item that was removed (which you have assigned to the variable removed, so you should return that).

After solving these issues, you’ll be able to pass the challenge.

Thank you very much! I’ve been overwhelmed by this challenge for days now and hadn’t been able to pass it until now. I have better understanding of what the question wanted me to do now!

1 Like

Yeah, in these situations it’s important to kind of reset yourself, forget everything you assumed and go through it again, step by step.
Once you solved a problem, it’s immediately much easier (obviously), and the reason probably isn’t some sort of complicated understanding, but instead a much more simple/precise understanding.
So most of the time it’s important to be able to relax and approach it from a very robotic way.

(at least for me, that has worked)