Skip to content

Instantly share code, notes, and snippets.

@saifsmailbox98
Last active June 24, 2017 22:09
Show Gist options
  • Select an option

  • Save saifsmailbox98/741a66dafb4bd7315974e82fead0916d to your computer and use it in GitHub Desktop.

Select an option

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]

💬

@saifsmailbox98
Copy link
Copy Markdown
Author

saifsmailbox98 commented Jun 24, 2017

I think you are using shift() twice.

@saifsmailbox98
Copy link
Copy Markdown
Author

function nextInLine(arr, item) {

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

@kramaniam
Copy link
Copy Markdown

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
Copy Markdown
Author

👍

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