Basic JavaScript: Stand in Line

Basic JavaScript: Stand in Line
0

#1

I really need to be explained to what I am missing. I am confused and can’t seem to figure out what exactly I am doing wrong and what to fix.

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  arr.shift();
  return arr.shift();  // 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));

#2

Hi,
How many items do you need to remove from the beginning of the array?

See comments in code


#3

Hello I have modified my code and made some progress:

function nextInLine(arr, item) {
  // Your code here
  testArr.push(item);
  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(item); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));```

But I am unsure of how to modify the line: 

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


#4

your problem is what youre returning. The last line in the assignment says The nextInLine function should then return the element that was removed. What youre returning is the item that you are adding to the end of the line. remember that the shift function not only removes the first value in the array but also stores it.


#5

From the instructions

Add the number to the end of the array,
then remove the first element of the array.
then return the element that was removed.


#6

okay so now you are not returning anything you need to return the value that you removed. Also you need to use the same name as the argument passed into the function parameters.
So where you put testArr you need to be putting arr. Also after the return statement you need to put arr.shift();
or you could assign arr.shift() to a variable and then return that.


#8

You were close the first time, the problem with that one was that when you ran arr.shift() it removed the first element in the array. Then when you called the function again in the return statement, it ran the function again and returned the next value in the function.


#9

Without giving the answer away, I’ll say this: Each time you call arr.shift();, it chops off the first value of the array you’re calling.


#10

Thank you for your responses, I finally got it and passed to the next sections. :slight_smile: