There is something I dont understand. Below is my solution for the challange for the specific topic and I completed it.
function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);
item = arr.shift();
return item;
// Only change code above this line
}
// Setup
const 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));
But why do I have to use arr to push and shift instead of testArr. My initial attempt at completing the challenge is as follows:
function nextInLine(arr, item) {
// Only change code below this line
testArr.push(item);
item = testArr.shift();
return item;
// Only change code above this line
}
And looking at the console it returns the same thing. But I know what I did was not quite right. WHY?
.
.
.
.
.
Challenge: Stand in Line
Link to the challenge: