Trying to understand where I went wrong with Stand in Line

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:

testArr is one example.

Your function needs to work for any array of numbers passed into it.

If I wanted to test your function with another array then it wouldn’t work because you are referencing the testArr.

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 myAwesomeListOfNumbers = [13, 15, 33, 47, 57];

When you use the arr parameter inside your function instead of testArr, then you have the ability to pass in any array of numbers when the function is called and your function will work correctly.

The goal of functions is to make them reusable.

Most people have.
It trips a lot of people up

1 Like