Stand in Line - How to use "Queue" in JavaScript

Tell us what’s happening:

I have no idea how to use “queue” in JavaScript. The instructions don’t mention if it’s a variable, a function, or what. I don’t know where to put it, what to put it with, or how to use it. Internet searches offer complex examples and commands not mentioned in this assignment or those previously.

Thank you,
Stephen

Your code so far


function nextInLine(arr, item) {
  // Your code here
  var queue = Queue(arr,item);
  return item;  // Change this line
}

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

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 10452.99.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.203 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line

It isn’t a variable or a function, you’re manipulating the array so it works like a queue. It is confusing here because the text makes it look like it’s an actual special thing by using queue formatted as code in the description

Edit: it’s a lot simpler than I think you’re thinking. The array arr is a queue of items (you don’t need to get super deep into reading up about queues - just think of it like a queue of people, hence ‘stand in line’). The front of the queue is the right-hand side of the array, the back is the left-hand side

You put the new item item at the front of the queue (on the right), and you remove the first item from the queue (on the left). That value you’ve removed, that’s what you return that from the function.

3 Likes

JavaScript doesn’t have built-in queue data structure, so you are implementing a function that acts similar to the queue.

Queue is simple:

  • it maintains First-In-First-Out order.
  • The operation that puts an item at the end is enqueue. i.e) Array[n + 1]
  • The operation that discards the item at the front is dequeue. i.e) Array[0]

So, nextInLine() is just a function that does both enqueue and dequeue in one round.
For example, when you call:

nextInLine(testArr, 6) // testArr = [1, 2, 3, 4, 5]

The function should return 1, which is the removed first item of the array. (Dequeue)
Also, it should change the testArr to [2, 3, 4, 5, 6], where 6 is the item added at the end of the array. (Enqueue)

Normally, you don’t implement enqueue and dequeue in one function; but, just do it for the sake of doing exercise.

2 Likes

@gunhoo93 and @DanCouper - Great explanations~! They helped me understand the concept. Ultimately, it allowed me to implement the code to get what I considered the desired result.

However…

My code keeps getting flagged as missing:

  • nextInLine([2], 1) should return 2

I don’t quite understand why… I have played around with the ‘console.log(nextInLine(testArr, 1)); // Modify this line to test’ arguments to no avail.

My current code to an entirety is:

This text will be hiddenfunction nextInLine(arr, item) {
// Your code here
arr.shift()
return arr.push(item); // Change this line
}

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

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

Any ideas of what I should be doing differently?

Thank you in advance.

You are returning the item that you pushed.
nextInLine() should return the first thing in the line that you discarded. (dequeue)

Push first, then return the result of shift. Also, have a look at what push returns (hint: it isn’t the value of an item in the array)

That indeed did return the [2,3,4,5]. However, I lost everything else. Looking at this the last hour or two, I am not truly understanding why I should push (add values to the end in order inputted) before I return my shift(remove first value of input).

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

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

This results in:
Before: [1,2,3,4,5]
1
After: [2,3,4,5]

It’s just what the exercise is asking you to do. There is no bigger reason.

Read the instruction and look at the test cases, the return value should be the first element that was in the argument array.

Your code should push the item and return whatever is in the first array.
Note that arr.shift() doesn’t take any argument.

suppose arr = [1, 2, 3, 4], item = 5

arr.push(item) // arr = [1, 2, 3, 4, 5]
return arr.shift() // return 1, and of course arr = [2, 3, 4, 5]
2 Likes

BAM! That makes the world of sense to me… When you add the arguement ‘(item)’ to the push, you are linking it to the original function with direction of what we are moving. Ultimately, the ‘shift()’ will put what we pushed onto the end of the array.

I got everything to work and believe I understand what is going on now.

Hm, not really…

arr.push(val) appends a value to the end of array.

let arr = []
arr.push(1) // [1]
arr.push(2) // [1, 2]
arr.push(3) // [1, 2, 3]
...

arr.shift() removes first element of the array and returns the removed element.

arr.shift() // 1, [2, 3]
arr.shift() // 2, [3]
arr.shift() // 3, []
arr.shift() // undefined, []

That is how I learned it in school, and how I originally anticipated utilizing push/shift.

The Nextinline bit is something I guess I need to look into further. :confused:

Thank you!

Yes, there does seem to be a typo in the instructions, as “queue” is not an element to be used in this assignment.

I learned a lot from your advice and then used the arr.push() and arr.shift() functions to get it right.

Thanks!