Stand In Line javaScript Exercise ! Stuck!

Stand In Line javaScript Exercise ! Stuck!
0

#21

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.

Does that make sense?


#22

Thanks Chad.

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!


#23

Hi there, I hope it’s okay if I also ask about this challenge too. ChadKreutzer said at the beginning of the thread that

First: testArr is what is being passed into your function as arr. You don’t want to operate directly on it. You want to operate on arr

How will I know that testArr is being passed into my function as arr on similar situations in the future? I don’t understand :sob:


#24

I’m stuck on the same one, but I think I can help here.

arr is being passed to the function inside of the ()

function nextInLine(arr, item) {

If we were to change it to function nextInLine(array, item) {

then we would address it with array.

Does that help?


#25

What about the var testArr = [1,2,3,4,5] ? I don’t understand how it is linked to function nextInLine(arr, item)


#26

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.


#27

@ChadKreutzer I have solved this problem thanks to your explanation. Thank you.


#28

function queue(arr, item) {
// Your code here
arr.push(item);
return arr.shift();; // Change this line
}


#29

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


#30

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.


#31

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. :+1:


#32

This is very helpful.

Thanks Chad.


#33

This was a cool exercise.


#34

Thanks a lot! I was also stuck in this exercise.


#35

Hey Thank you so much for the stand in line exercise explanation! I was truly lost until i read this over and rewrote the code a couple of times!


#36

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?


#37

Yup.

But remember, each time you call arr.shift() it does it’s thing.


#38

Great, thanks.

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)

Thanks!


#39

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);

#40

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 :frowning: