Doing a recap for myself so far passing all the lessons without any problems but, i just can’t to seem to understand this one.
They asking to:
Add the number to the end of the array
which should be done with push since it pushes one number
then remove the first element of the array.
which should be shift() since it always removes the 1st element of an array.
Then i should use array arr for short since it contains a number. Item is more less likely to do so.
Yet when i try this it doesn’t work
Your code so far
function nextInLine(arr, item) {
// Only change code below this line
arr.push()
arr.shift()
return item;
// 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/80.0.3987.149 Safari/537.36.
Add the number to the end of the array, then remove the first element of the array. so this and
The push() method needs to know what to add to the end of the array (i.e. you have to pass it the value you want tacked onto the end of the array).
should make:
arr.push()
The nextInLine function should then return the element that was removed.
The shift() method returns what it removed from the beginning of the array so you need to save that value in a variable
arr.shift()
but yet that isn;t working why not?
Here’s a simple tutorial I found that explains Array.push() and Array.shift() (as well as a few others):
Hopefully the examples make it clear how to use push() and shift(). As I mentioned before, you have to tell push() what value you want to push onto the end of the array. And shift() will remove the first value in the array and even better, will return that value. That is the value you want to return.
function nextInLine(arr, item) {
// Only change code below this line
var arr.shift(item); //the argument = item. also a variable is added
arr.push(item);
return item; //should't the item be the thing that is returned since it holds the result
// 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));
Thanks as alway’s for the good explanation. I got it now
After removing the 1st line : var arr.shift(item)
i should have used the return value instead using shift