Basic JavaScript - Stand in Line

Tell us what’s happening:
Completely stuck on this bit, I think I’m overlooking something obvious. What did everyone else do?

Your code so far

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

Your browser information:

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

Challenge: Basic JavaScript - Stand in Line

Link to the challenge:

Could you explain why item is being returned? Goal is to add item to the array, and return the first element from array.

Tell us what’s happening:
When use this ask it to return item, it helps me pass half of this section, when I replace item with firstElemRemoved for return, it passes me on the other half. How can I make it return the firstElemRemoved and item and pass all of it altogether?

Your code so far

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

Your browser information:

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

Challenge: Basic JavaScript - Stand in Line

Link to the challenge:

I don’t think the return value of push does what you think. I would review how push works.

Add the number to the end of the array, then remove the first element of the array.

The order that your code is executed in must exactly match the instructions.

So the ‘item’ is the next number to be pushed to the end of the array. I’ve worked out how to do this with this code:

let firstElemRemoved = arr.shift(0);
console.log(firstElemRemoved);
let nextElem = arr.push(item);
console.log(nextElem);
return item;

but then the instructions want me to also do the following:

nextInLine([2], 1) should return 2
nextInLine([5,6,7,8,9], 1) should return 5

I’ve replaced ‘return item’ with ‘return firstElemRemoved’ which it accepts but then it doesn’t calculate the item removed as I’ve asked it to return the number removed too. Is there a way to do both? Or am I missing a line of code which combines both?

The instructions don’t ask for this.

I’ve managed to do that but I’m now struggling to test the function, specifically the following:

nextInLine([2], 1) should return 2
nextInLine([5,6,7,8,9], 1) should return 5

Did you fix this yet?

I’ve changed the order to add the number to the end and then remove the first element. My understanding of the instruction is to use the number ‘item’ to add it to the end of the array, is that not correct? The ‘return item’ was already added to the task, I didn’t add it.

What is your latest code?

also note, shift takes no arguments:

I’ve managed to pass it, I’ve changed the return function from ‘item’ (which is what they added) to firstElemRemoved - as follows:

function nextInLine(arr, item) {
  // Only change code below this line
  let nextElem = arr.push(item);
  console.log(nextElem);
  let firstElemRemoved = arr.shift(0); 
  console.log(firstElemRemoved);
  return firstElemRemoved;
 
  // 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));
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.