Stand in line: can’t work it out

I’ve checked the forum and looked online but I’m still stumped as to this one.

Push adds an item to the end of an array, and shift removes and returns the first element of the array. So why isn’t combining them like this working? What am I missing?

I’ve tried swapping their order to no avail.

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

// Setup
const 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 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15

Challenge: Stand in Line

Link to the challenge:

HI @EllaGriff !

You’re close.

I would look close at the MDN docs on shift

Remember that the directions want you to return the element that was removed.

Right now you are just returning item.

Once you fix that small error, then it will pass

Hi,
I’ve read this but still don’t understand.
I thought item was the element in question? If item isn’t the element, what is and how do I access it to return it?

To my understanding of the lesson I shouldn’t be using anything other than the parameters arr and item to pass this. And I shouldn’t be declaring any new variables.

You wrote this

But you don’t want to just return the item.
You want to return the element that was removed.

The way shift works is that it removes the first element of the array and returns that removed element.

Make sense?

I don’t want you to declare any new variables. :slight_smile:

You can add this console.log to your code to see what the return value is

arr.push(item);
arr.shift(item);
console.log(`This is what the shift method returns: ${arr.shift()}`)

Once you understand the return value from the shift method, then you will be able to adjust this line here to return the correct result

Hope that helps!

Sorry I still don’t get it.

Shift seems to return numbers but I don’t understand why those numbers are important. I don’t know what they mean in the context of my code.

Sometimes it returns undefined meaning the function doesn’t have a return value. But I don’t know what I’m supposed to return to make the function work still.

Do I need bracket or dot notation here or am I over thinking this?

Also I’m still not sure what the element is. If item isn’t the element what is the element. The numbers in testArr?

Which number?
That’s the important part, what’s the returned value?

The “element” is the one mentioned when it says “remove the first element in the array and return it”
item is the input and you need to do something with it, but it’s not the output

1 Like

I didn’t need to put item in shift so removed it.

It is removing the 6: then adding it to the end. The one is also removed but not returned. Then the 6 is removed and added again. So the numbers aren’t looping correctly. But I don’t know how to fix this or why it isn’t working.

// console output
Before: [1,2,3,4,5]
6
After: [2,3,4,5,6]
Before: [1,2,3,4,5]
6
After: [2,3,4,5,6]
function nextInLine(arr, item) {
// Only change code below this line
arr.shift();
arr.push(item);
return item;
// Only change code above this line
}

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

We still need to find what is the value that shift return

const a = [1, 7, 9];
a.shift(); // what's the output?

const b = ["a", "dove", "pizza", "tree"];
b.shift(); //what's the output?

Note that only this is for a function call,

you get the six lines output with three lines for each time in which the editor code is run with the tests

The number 1 and the string a are the output.

So my code should take 1 from the front as output and put it at the back of the queue. But it’s doing things in reverse?

you may need to read again the instructions.
You have arr, which is the array, and then ítem - what is the role of the item parameter?

The logic you have described ignores the item parameter, it couldn’t be right

It’s a parameter for that function and needs an argument passed to it. It is the parameter for a number passed as an argument to the function I think.

Is the problem that there’s no argument passed to item and I need to pass one?

I don’t know what it’s role could be other than to have an argument passed to it. Or what argument it needs passing to it.

Just to be clear, your original code was super, super close. :slight_smile:

If the shift method returns the element that was removed and the directions want you to return the element that was removed, can you see what your function should return?

Yes I do and I got it to work!

I didn’t realise I could put one of those (not spoiling for others) in a return for some reason. :sweat_smile:

1 Like

The question was about what you have to do with the value of it, you don’t need to pass anything to it as that is done by the tests and the function call already written in the editor

1 Like