Queue challange javascript

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:

Nope, nevermind what i sent

Because you want a function that can work WITH ANY array.
testArr is literally just a “test array” to see if the code works.
However if the function is provided another array - as can be seen in the test cases → your code would ignore it. It only refers to “testArr”, regardless of the actual array provided to the function.

The console output looks the same, because you only test it with testArr.

It’s like spellchecking a word but only testing with “ca3t”. Ofcourse if you remove the 3, it will be “cat”. But if you never test it with “d2og”, you will not notice how there can be other things wrong with a word besides a 3 on index 2.

2 Likes

okay, that cleared things up for me, thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.