Why is shift() not a function

Tell us what’s happening:

Your code so far


function nextInLine(arr, item) {
// Only change code below this line
arr = arr.push(item);
var removedItem= arr.shift();
return removedItem;
// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

Hello @prak3429,

If we have a look at the line of the below

arr = arr.push(item);

the variable “arr” is converted to a type number, so the “arr” is no longer an array.

This is because, the push() function return the length of the array.

And then at the second line:

var removedItem= arr.shift();

You can no longer perform the shift() operation, because arr is no longer an array but a number. Hence the error.

If you don’t reassign the variable “arr”, you will be fine.

Thanks.

4 Likes

Hey @prak3429!

I am also going to link the documentation so you can see examples of arr.push and what it actually returns.

Hope that helps!

3 Likes