Skip to content

Instantly share code, notes, and snippets.

@saifsmailbox98
Last active June 24, 2017 22:09
Show Gist options
  • Save saifsmailbox98/741a66dafb4bd7315974e82fead0916d to your computer and use it in GitHub Desktop.
Save saifsmailbox98/741a66dafb4bd7315974e82fead0916d to your computer and use it in GitHub Desktop.

You first need to insert the item to the end of the array arr using the method push() on the array arr. And then remove and return the first element of the array using shift() method on the array arr.

  • push(): Add items to then end of an array. push() returns the new array length.
  • shift(): Remove an item from the beginning of an array. shift() returns the removed item.
  • pop(): Remove an item from the end of an array. pop() returns the removed item.
  • unshift(): Add items to the beginning of an array. unshift() returns the new array length.
var myArray = [1,2,3,4,5];

myArray; //[1,2,3,4,5]

myArray.push(8); // 6 (new length)

myArray; //[1,2,3,4,5,8]

myArray.shift(); // 1 (removed item)

myArray; // [2,3,4,5,8]

💬

@kramaniam
Copy link

kramaniam commented Jun 24, 2017

Hey. I am having a really hard time understanding this challenge. Could you help me?

@saifsmailbox98
Copy link
Author

saifsmailbox98 commented Jun 24, 2017

push() can be applied to an array and requires an element to be inserted into the array.

arr.push(item);

Similarly shift() removes the first element of the array and returns it.

arr.shift(); // removes the first element and returns it.

So you can do

return arr.shift(); // removes the first element and returns it from the function

@kramaniam
Copy link

kramaniam commented Jun 24, 2017

Okay...
So the code looks like this:

function nextInLine(arr, item) {
  
  arr.push(item);
  arr.shift();
  
  return item; 
}

What should I do next?
And could you also explain a little more what it means for the shift() method to return the element that it removes?

@saifsmailbox98
Copy link
Author

saifsmailbox98 commented Jun 24, 2017

When you write
arr.shift();
that line of code will get replaced with the removed element.

arr = [5, 6, 7];

arr.shift(); this line will change to 5

so you can directly return arr.shift();

@kramaniam
Copy link

kramaniam commented Jun 24, 2017

Okay... It's starting to make sense now.

I have changed the line that requires modification from 'return item;' to 'return arr.shift();', but it doesn't complete the challenge.
Is there something I am missing or getting wrong?

@saifsmailbox98
Copy link
Author

saifsmailbox98 commented Jun 24, 2017

I think you are using shift() twice.

@saifsmailbox98
Copy link
Author

function nextInLine(arr, item) {

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

@kramaniam
Copy link

kramaniam commented Jun 24, 2017

My goodness! Yes I was using shift() twice.
It now works, and I completely understand what has happened (although it makes me feel kind of dumb that I didn't figure it out earlier :\ )
I am very grateful for your help.

@saifsmailbox98
Copy link
Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment