Basic JavaScript - Stand in Line

Tell us what’s happening:
I’ve searched the forums and used the “Get Help” button, which I then watched the video and visited “Get a Hint”. However, the code below still fails.

Error I am receiving:

  • Failed: nextInLine([2], 1) should return 2

  • Failed:nextInLine([5,6,7,8,9], 1) should return 5

Your code so far

function nextInLine(arr, item) {
  // Only change code below this line
  return testArr.shift();
  // 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));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Stand in Line

Link to the challenge:

Where are you getting the testArr from?

Its provided in the setup

Ahh, I see. I kind of wish they didn’t include that, it can be misleading.

Your function shouldn’t work on just that array. It should work on any array passed into it.

1 Like

Thank you. I figured it out. I assumed they wanted me to target that specific array of testArr. The headache. My code now passed.

However, I don’t quite understand the logic. Why/How are the parameters of the functions able to be entered into the { }? I thought parameters were just place holders until we gave those placeholders an action to execute?

Typing out loud:

  • Assuming the provided “console.log(nextInLine(testArr, 6));” is defining the placeholder of arr and item. arr = testArr and item=6. — If this is true why is console.log defining placeholders? Shouldn’t that be done within { }

  • When we run .push(item) this action references the number 6 transforms the array into
    [1, 2, 3, 4, 5, 6]

  • Then we .shift() which transforms array into [ 2, 3, 4, 5, 6]

Any insight appreciated. Thanks!

I don’t think I understand this question. Can you be a little more specific?

The console.log is being used merely to show you the value the function is returning. It has nothing to do with the actual calling of the function.

1 Like

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