I need some clarification plz!

Tell us what’s happening:
Describe your issue in detail here.
So I’m kind of stumped on this section here, can anyone explain it to me in a nutshell how this process works? I can see how the answers mesh together in a way but the idea of push and shift is a bit confusing, and adding onto that, i’m unsure what effect () has and what it means. I don’t recall learning shift and push in the previous sections but I may have overlooked it :S

  **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_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15

Challenge: Stand in Line

Link to the challenge:

I edited your post to include spoiler tags for those who have not worked on this problem yet.


The push method was introduced in this FCC array lesson
The shift method was introduced in this FCC array lesson

The push method adds elements to the end of an array while the shift method removes elements from the beginning of the array.

In this challenge, the first step is to add a number to the end of the array.
So that is why you use the push method.
In our function, the item parameter represents a number.

The second step of the challenge is to remove the first number of the array.
So that is why you use the shift method.

Here is what your code looks like using an example using the var testArr = [1,2,3,4,5]; and this function call nextInLine(testArr, 6)


function nextInLine(arr, item) {

// first Add the number to the end of the array 
arr.push(item);

// your array will now look like this
//[1, 2, 3, 4, 5, 6]

//remove the first element of the array
var removed = arr.shift();

// your array will now look like this
// [ 2, 3, 4, 5, 6 ]

// return the element that was removed
return removed;

}

var testArr = [1,2,3,4,5];
nextInLine(testArr, 6);
2 Likes

Hey Sherif123,

what jwilkins.oboe said is absolutely correct.

push and shift are functions that you can apply on arrays in JavaScript. They are literally dozens of them, and it is normal that you forget something from time to time.

A good developer does not remember every function in a programming standard library, but he knows where to find them. For example, you can find a comprehensive documentation about JavaScript standard library methods on the Mozilla Developer Documentation:

best,
Dennis

1 Like

That’s awesome thank you so much! i’m just wondering though why there is () at the end? what does that represent?
thank you again :slight_smile:

It represents a method call.
I found a good article on codeacademy that breaks down method and function calls with code examples.

1 Like

God bless you thank you so much :smiley:

2 Likes