Stuck on this Javascript Exercise

Hey friends, I’m pretty new to coding and even the baby stuff isn’t always easy for me … I’ve been stuck on this exercise for a while and no matter how much I stare at it, I just can’t parse it. I would appreciate some guidance, so I can get past this stumbling block and on to Booleans!

Here is the text of the challenge, and then a link:

In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.

Write a function nextInLine which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The nextInLine function should then return the element that was removed.

I suggest you do a search in the forum. Questions of this nature seem repetitive. Chances are it has been answered already. Type in the name of the challenge in the search box and see where it leads. For example, it was discussed here not long ago:

Good luck!

This simply is asking you to use a couple of built in array functions. Take a look at this ref for a list and descriptions of those functions.
http://www.w3schools.com/jsref/jsref_obj_array.asp

1 Like

Ah, thanks - this confirms that I was actually on the right track! I was looking at this reference:

http://www.w3schools.com/jsref/jsref_obj_array.asp

which has a table called “Methods” - and I was writing code that passed some but not all of the tests.

I’ll tinker more later - thanks for the helpful response!

1 Like

You’re very welcome!

w3schools gets a lot of flack but I use it often for quick references. Sometimes MDN doesn’t give me what I’m looking for.

Hi there, I’m probably not supposed to show you the solution, but sometimes it’s the only way to understand something, so here it is :slight_smile:

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

// Test Setup
var testArr = [1,2,3,4,5];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 5)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));