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: