Stand in Line yup im an idiot

Tell us what’s happening:

Doing a recap for myself so far passing all the lessons without any problems but, i just can’t to seem to understand this one.
They asking to:
Add the number to the end of the array
which should be done with push since it pushes one number

then remove the first element of the array.
which should be shift() since it always removes the 1st element of an array.
Then i should use array arr for short since it contains a number. Item is more less likely to do so.
Yet when i try this it doesn’t work

Your code so far


function nextInLine(arr, item) {
// Only change code below this line
arr.push()
arr.shift()
return item;
// Only change code above this line


}

// Setup
var 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

The push() method needs to know what to add to the end of the array (i.e. you have to pass it the value you want tacked onto the end of the array).

The shift() method returns what it removed from the beginning of the array so you need to save that value in a variable.

2 Likes

It’s a bit hard to explain since it’s abstract thinking you will need to do here. Try to think like a computer and not as a human being here.

Add the number to the end of the array, then remove the first element of the array. so this and
The push() method needs to know what to add to the end of the array (i.e. you have to pass it the value you want tacked onto the end of the array).
should make:
arr.push()

The nextInLine function should then return the element that was removed.
The shift() method returns what it removed from the beginning of the array so you need to save that value in a variable
arr.shift()
but yet that isn;t working why not?

We would need to see your updated code before we could answer your question.

It’s excat the same though. There is no update since your previouse answer just lead me to the same conclusion. It’s called being stuck

You didn’t follow @bbsmooth’s instructions.

You did not

  1. Add an argument to push(). push() doesn’t have anything to push.
  2. Add a variable to save the result of shift().
  3. Return the variable that contains the result of shift().
1 Like

Here’s a simple tutorial I found that explains Array.push() and Array.shift() (as well as a few others):

Hopefully the examples make it clear how to use push() and shift(). As I mentioned before, you have to tell push() what value you want to push onto the end of the array. And shift() will remove the first value in the array and even better, will return that value. That is the value you want to return.

Okay i try that

function nextInLine(arr, item) {
  // Only change code below this line
var arr.shift(item); //the argument = item. also a variable is added
 arr.push(item);

  return item; //should't the item be the thing that is returned since it holds the result

  // Only change code above this line
  

}

// Setup
var 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));

shift doesn’t need an argument

writing var arr.shift(item) is nonsense, as you are not declaring any variable here

shift returns the item it removes, which you should then return

you are just returning the function parameter, instead

2 Likes

Thanks as alway’s for the good explanation. I got it now
After removing the 1st line : var arr.shift(item)
i should have used the return value instead using shift