Arr.push() + arr.shift() QUE structure help?

Tell us what’s happening:

Need help to understand.

Why does the ‘item’ not get inputed into both like this

arr.push(item);
arr.shift(item);

or why does ‘return’ no get inputed into both like

return arr.push( );
return arr.shift();

if someone can help me understand why the two lines have their differences I’d greatly appreciate it.

Your code so far

  arr.push( item);

return arr.shift();


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

return item;
// Only change code above this line
arr.push(item);
return arr.shift();

}

// 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_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

shift

They don’t do the same thing because they are opposite functions.

1 Like

I was having trouble understanding the syntax, I think your explanation helped…

is it right that ‘arr.push(item)’ requires ‘item’ because its actually “pushing” the item to the end of the array

while ‘arr.shift()’ does not require ‘item’ between the brackets () because the function is removing an element from the array and ‘item’ has nothing to do with that?

and the “return” only needs to be called once at the end of the function?

-Thanks for your response!

Correct. The parameter (item in this example) is the thing you want to add to the array.

Correct. shift does not take any parameters.

return is always the last thing that happens. It ends function execution. As soon as a return statement is hit, no code after it will be executed.

1 Like