Why isn't this working? (Queue function)

Tell us what’s happening:

Hello all.

just wondering why this code isn’t working? It seems to fulfil the critera: adding ‘item’ onto the end of the array (item is in this case ‘number’) and removing the first item from the array, returning this value. Can’t spot my error!)

Your code so far


function nextInLine(arr, item) {
// Only change code below this line

arr.push(item)
var removed = arr.shift;

return removed;
// 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 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

.shift() is a function and needs the () at the end.

Thankyou! noticed that myself just after posting. I do however, have a follow up question. To be honest - this problem has thrown me a bit, as although i understand the bit of code i’ve used, i really don’t understand the outcomes (posted as a screenshot - even though i satisfied all of them!

Screenshot 2020-05-05 at 17.00.39|690x291

Could you advise me on what any of these mean?

Thanks!
Matt

nextInLine takes two arguments, an array arr and a number item.
With the code you’ve written, the function will push the item value to the arr (push appends the value to the end of the array). The function will then shift the array (shift removes the FIRST value of the array) and store that value in removed. The function returns the value of removed.
nextInLine([], 1) returns 1 because the array is empty. It pushes 1 to the array, which results in [1]. Then it shifts the array, removing 1 and returning it.
Does that make sense?

1 Like

Thank you so much, really appreciate you taking the time to explain that. I feel as though i understand what the function is doing now.

it’s just the last line: After nextInLine(testArr, 10) , testArr[4] should be 10
I’m still struggling with. No idea what it means!

EDIT: i understand this now! calling the function using the defined variable ‘testArr’ returns 10, and changes testArr to [2,3,4,5,10], testArr[4] is asking what is the 5th item in the array, which is 10.

I’m also confused at the code:

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

I’m not sure if any of this is supposed to make sense as there are things in there we haven’t covered yet, although i feel like i’m still missing some of the ‘why’ for this problem, if that makes sense.

Thanks again
Matt

This writes the testarr value to the console with “Before:” and “After:” so you can see the array before the function is called and after the function changes it.