Stand in Line - my solution doesn't work - why?

Tell us what’s happening:

I’m wondering why my solution doesn’t pass the test: After nextInLine(testArr, 10), testArr[4] should be 10.

Can someone explain it to me, please?

Your code so far

function nextInLine(arr, item) {
  // Your code here
  var newArr = arr.concat(item);
  var result = newArr.shift();
  return result; // Change this line

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine([1,2], 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:

Some of these questions are bugged. Answer looks fine to me. Just move on and check later to see if they fix it.

You are supposed to be modifying arr. Instead you have created a new array because concat returns a new array after combining arr and item.

Yeah, but in instruction it wasn’t clearly stated that I must modify array and in the console my code works.

Run nextInLine(testArr, 10) and then put console.log(testArr[4]) and see if you get 10. My guess is you get 5, because you are not adding 10 to arr and you are not shifting the first element (1) from arr.

You are right. I have tested it before step by step like this:

var testArr = [1,2,3,4,5];

var newArr = testArr.concat(10);

(6) [1, 2, 3, 4, 5, 10]

var result = newArr.shift();


(5) [2, 3, 4, 5, 10]


Now I get it why I should modify, not create a new array :wink:


Yeah, but in instruction it wasn’t clearly stated that I must modify array…

Well, it kind of does. “Add the number to the end of the array, then remove the first element of the array.” It talks about “the” array, not a new one.

And then it goes on to say, “The nextInLine function should then return the element that was removed.” So, it tells you exactly what to return. And since you aren’t returning the array, the only way the code could have access to that modified data is if you change the original. (In JavaScript, non-primitive types are always passed by reference so any changes you make inside the function are affecting the original.)

An important skill in programming is paying attention to minute details. As a professional coder, you will often have to suss out and infer requirements from vague statements.

1 Like

@kevinSmith Many thanks for explanation. I’m not a native English speaker - it seems that I must also improve my English language skills :wink: