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
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.
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
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.
Just to be clear, your original code was super, super close.
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?
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