Basic JavaScript - Stand in Line

Hi.

I’m having trouble with this challenge:

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line/

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);
  testArr.shift();
  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:

arr.push(item);
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.

2 Likes

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.

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: