I looked at the solution because I couldn’t figure out what I was doing wrong and I found out I was pretty close.
I know now that instead of testArr, I had to put in arr to replace the testArr I had in the function. But I’m trying to understand why.
I think it was this line in the exercise that messed me up:
Add the number to the end of the array, then remove the first element of the array.
I think what I thought was to add the number (item) to the array - which in my mind was the testArr, not just any array which I’m realizing now.
Ok, I think I’m understanding the code now. I hope no one minds me sharing this just to see if anyone else has any advice on me to solidify this code for myself. Or has anyone else made the same mistake I did?
function nextInLine(arr, item) {
// Only change code below this line
testArr.push(item);
let removed = testArr.shift();
return removed;
// Only change code above this line
}
// Setup
let 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));
console.log(nextInLine([], 1))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Stand in Line
Link to the challenge: