Basic JavaScript - Stand in Line


I’m having trouble with this challenge:

As I understand, I’m supposed to add a number to the end of the array then take a number off the beginning of the array.

Here’s my syntax:

function nextInLine(arr, item) {
  // Your code here
  testArr.push([], 5);
  return item;  // Change this line

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

I’d be grateful for any help.

So it’s asking you to add the given element to the end of the given array (see your parameters?), then remove the first element from that array. AND… return that removed element.

how about simply:

let removedItem = arr.shift();
return removedItem;

Note, you should not be accessing testArr directly in your function. You are being passed an array into your function, that should be the only thing of which your function is aware. By hard-coding testArr into your function, it loses pretty much all flexibility. I couldn’t create an array on the fly and pass it to your function. I couldn’t send you a value and have your function add it – you’ve hard-coded the value into your function.


Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, try to just stick with describing an algorithm or pointing out problems in the OP’s code instead of posting the solution.

Thank you.

Thank you, I wasn’t sure how to do the blurring-out thing, going into edit the code showed me. Appreciate the patience, and the guidance.

Thank you for your help.

In general, please don’t post solutions as responses to OP’s questions (even if blurred). Anyone can post a solution, but not everyone can post a reply which allows an OP to create their own solution without seeing the solution code first.

Your explanation regarding “hard-coding” was super helpful! That’s exactly what I was doing ! Thank you!

1 Like

All of the curriculum up to this point was “hold me by my hand” too easy and all of a sudden I have to go look on the forum for a solution. :worried: