Basic JavaScript - Stand in Line

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

function nextInLine(arr, item) {
  // Only change code below this line
  arr.push(5)
  arr.unshift(1)
  


  
  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));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Stand in Line

Link to the challenge:

Try this:

function nextInLine(arr, item) {
  // Only change code below this line
  arr.push(item);
  const removed = arr.shift();
  return removed;
  // Only change code above this line
}
1 Like

Do you just do this challenge?

Did it work?

No, I did it earlier.

It worked but has two step what are really dificult for me

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

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

  • Test nextInLine([2,1]); runs.
  • The nextInLine function is called. arr becomes [2]. item becomes 1.
  • arr.push(item); Pushes 1 to [2]. So arr is now [2,1].
  • const removed = arr.shift(); removes the first element. So arr is now [1]. 2 has been removed and is stored in removed.
  • return removed; 2 is returned.

Note: You don’t actually need the variable removed. The element removed can be returned directly using return arr.shift();.

1 Like

Found the explanation here:

Very very thank you, i do and result thanks @Muzzammmill

Done i do this
right now
Thanks

1 Like

I can’t figure out why my code is not passing. I have tried everything and can’t see why I’m not passing any tests. Here is my code:

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

None of the tests pass, so I’m getting all errors.

Nevermind. I figured it out.

how did you figure it out. I’m hooked on this too

If you have a question about a specific challenge as it relates to your written code for that challenge, and you’ve tried to solve it at least 3 times so far and still need some help, just click the Ask for Help button located on the challenge (it looks like a question mark).
This button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

For me, the js page in the index folder was linked incorrectly.