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.
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.
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.