Help needed with Basic Javascript: Stand in Line

I’m working on Basic Javascript: Stand in Line.
I looked went ahead and looked at the solution because I didn’t know how to do it.

Questions below.

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

Code Explanation

  • Push item at the end of arr .
  • Call the shift() method on arr to get the first item and store it in removed .
  • Return removed .

Example Run

  • Test nextInLine([2,1]); runs.
  • The nextInLine function is called. arr becomes [2]. item becomes 1.
  • arr.push(item); Pushes 1 to [2]. So arr is now [2,1].
  • var removed = arr.shift(); removes the first element. So arr is now [1]. 2 has been removed and is stored in removed .
  • return removed; 2 is returned.

Note : You don’t actually need the variable removed . The element removed can be returned directly using return arr.shift(); .

Questions:

  • var removed = arr.shift(); removes the first element. So arr is now [1]. 2 has been removed and is stored in removed .
    var removed
    //‘removed’<---- is ‘removed’ a variable being declared?
    //arr.shift()<----is it necessary to have ‘arr’ in front of shift for it to work?
    //arr.push(item)<----what role does ‘item’ play here?

I am still a beginner in JS. If someone could explain answer, it would help me understand the problem.

Hello and welcome back to the forums~!

Let’s see if I can answer your questions.

Yes. The var keyword is used to declare a new variable. In this case, removed is being declared and initialised with the value of arr.shift().

Yes. The .shift() method removes the item at index 0 of an array. The syntax is array.shift(), where array is the array you want to remove the item from.

item is the parameter (like a fancy variable) of the nextInLine function. The .push() method takes one argument: The element to add to the end of the array. So arr.push(item) says “take the value given to the function in the item parameter, and put it on the end of the arr array.”

Thank you for the thorough answer!

I’m wondering about those parameters.
Regarding ‘array.shift()’, why couldn’t it be ‘item.shift();’
and vice versa for ‘arr.push(item)’? Could it be the other way around?

item is not an array, so you would get syntax errors. :slight_smile:

arrayToTarget.shift()
arrayToTarget.push(itemToInsert)

Thank you for your reply :slight_smile:

I think it’s dificult because I think of array as a list in a bracket. So, with push() and shift(), I think of numbers/letters being pushed in and shifted out.

Based on your reply, I’m thinking that (arrayToTarget, itemToInsert) are like a storage?

Sorry… for being so confused… but your explanations are very helpful!

This is pretty accurate. Let’s look at an example:

//first we define an array
const myArray = [1, 2, 3, 4];

//now let's make an item to insert
const myItem = 5;

// put that item in the array
myArray.push(myItem)
/*here we are saying take the value of myItem (which is 5)
and stick it at the end of myArray.*/

//what does the array look like now?
console.log(myArray) //prints [1, 2, 3, 4, 5]

//what about removing the first item?
myArray.shift()
// notice that shift does NOT take a parameter

//let's look at the array again
console.log(myArray) //prints [2, 3, 4, 5]

Wow… thank you so much!!

Yes, that makes a lot of sense :smile:

1 Like

Glad I could help! Happy coding! :sunglasses:

2 Likes

hello and thanks for the answer,
I wrote something different and I’d like to show it to you below:
arr.push(item);
item=arr[0];
arr.shift();
return item;
is it weak to write it in this way?