Basic JavaScript - Stand in Line

Basic JavaScript - Stand in Line
0

#1

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.


#2

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.


#3

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.


#4

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.


#5

Thank you for your help.


#6

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.


#7

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