nextInLine([5,6,7,8,9], 1) should return 5, anyone pls explain

Tell us what’s happening:

Your code so far


function nextInLine(arr, item) {
// Only change code below this line
   testArr.push(item);  
   return testArr.shift();
 // Only change code above this line


}

// Setup
var 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/87.0.4280.88 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

Hi @pmwahlang1!

This function is great if we only plan on having it work for just testArr. But it would be better if we could reuse this function on other arrays beside testArr.

Right now you are only using one of the parameters in your answer. It would be nice if you could use both of them.

Hope that helps!

1 Like

The [5,6,7,8,9]
I tried using this

     testArr.push(item);  
     return testArr.shift();
     testArr.push(item);  
     return testArr.shift();
     testArr.push(item);  
     return testArr.shift();
     testArr.push(item);  
     return testArr.shift(); 

but…
I’m confused with the whole function;

Thank you jwilkins i finally figured it out. Using this.

   arr.push(item);
  return arr.shift(item);

I didn’t use the arr of the function instead I used testArr. Sorry for the stupid question. VERY new to JS and will need more help from you. Thank you

This worked for me.

function nextInLine(arr, item) {
    arr.push(item);
    return arr.shift();
}

var testArr = [1,2,3,4,5];

console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Hi @pmwahlang1!

You’re almost there. Just a small issue.

In your function, you have 2 parameters, arr and item but the first parameter is not used.

So, instead of this:

testArr.push(item);
return testArr.shift();

Change it with this:

arr.push(item);
return arr.shift();

Hope this helps! If it doesn’t work for you then feel free to answer me amytime.
@pummarinbest