return arr.shift() doesn’t return arr. Rather it returns the value we get from arr.shift()
The shift method removes the element at the zeroeth index
and shifts the values at consecutive indexes down, then returns the removed value. If the length property is 0, undefined is returned.
I had to reread this about 5 times to finally get it, but I did and now I can move on. It is funny how you will add your own meaning to the words, even though it is not there. You are right it was so simple it was hard!
Thank you Chad for your clear explanation however do you mean “Using push(jones) on bob…” instead of “push(x)”? I’m a little confused as I too am stuck on this problem.
Good grief I was able to complete this this challenge by reading through all the post here but I don’t think I fully grasp everything going on in the challenge.
If you look at the bottom it tells us to modify the second to the last line to test, in the comments
Your explanation really help me understand about return syntax. I always thought return as a way to end a function. But now i realized that the function as it’s name, to return a value. Such a DUH moment for me. thank you for the enlightment.
Thanks a lot! This really helped me out… I’ve been breezing through these questions without a problem, then for some reason this one stumped me… Guess I was over thinking it.
First off, thanks a ton for taking the time to spell this out on detail. Can you please tell me know how you would solve this if you did a variable assignment? You’d put arr.shift() into a variable, and then return that variable?
Her’s what it looks like when I assign a variable to the removed element of the array:
function nextInLine(arr, item) {
// Your code here
arr.push(item);
var removed = arr.shift();
return removed;
// 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));
Would you say this is a reasonable way to do it with a variable? Does that code make sense? (I checked and it works)
It’s used purely to test the function. Look at the console.log statements and you’ll see how but just incase you still can’t see the connection, here is how the function would look like without the console.log statement
function nextInLine(arr, item) {
// Your push and shift code
}
nextInLine(testArr, 6);
goodness. I wish I had come across this thread when I was doing this challenge, at about the same time. I have been trying to get the concept of “Function” in to my thick brain, since beginning of the year. I have read about them.
Now I understand. You should consider doing a blog.
OHH… I still have problem understanding callback functions