Return the element that was removed and queue?

Tell us what’s happening:
Got stuck again. I am on Basic JavaScript: Stand in Line.

Here are the instructions:
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 the array.

The nextInLine function should then return the element that was removed.

It also introduces queue which I don’t understand and will definitely have to look up like I had to do for return.

This is what is states about queue:

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.

So is this saying that I should write code within the function as the new instructions at the top and old in the bottom. I sorry if I am not explaining this well
For example like this down below

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

The instructions said to add a number at the end of the array and to remove the starting number in the array. So is it saying that I need to but the first step at the bottom and the next step at the top like I wrote it above? Did I write this code right too?

Also it is asking for me to return the number in the array that was removed.
Should I assign my code to a variable then try returning it that way?
like this

function nextInLine(arr, item) {
  var removedArr = arr.shift();
  return removedArr;
  arr.push(item);
  return item;

I think the curriculum covered this so i will go back and check.

Also return is a way to bring a variable out of the scope limitation right. So that it can be used like a global variable right?

In the meantime I will just look up a video explaining what queue is and I understand if you find my question too confusing to answer. Sometimes it helps me to just type out what I don’t know lol.

Also
Your code so far


function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);
arr.shift();
return item;
// 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_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

This snippit is close, but the first time your code has a return statement, the function will do nothing else.

1 Like

Okay I got the code right but I still don’t know how queue relates to what I just wrote. I didn’t change the order of the code either as you can see down below. I will definitely have to look up queue to find out what that is…

function nextInLine(arr, item) {
  // Only change code below this line
  arr.push(item);
  var removedArr = arr.shift();
  return removedArr;
  // 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));

wow that was a fast reply thanks very much! I noticed that when I tried that out in the code box. The rest of the code under was blurred out. I will have to read up about return some more because I still find it confusing but thank you for telling me!

A queue is like a line at a checkout. You join a line at the end, and you exit at the head of the line.

1 Like

oh okay that makes sense. So queue is a concept that describes how the code I write is being processed then? The code at the top is process first then the code under is next in line maybe?

Eh, a queue is more of a type of data structure. In this exercise you treat the array arr like a queue, where elements are only added at the end and removed at the head.

1 Like

oooh okay I was pretty off. Thanks I got it now lol!