I am working a question on FreeCodeCamp. I need to add a number to the end of the array, then remove the first element of the array. The function called nextInLine should return the element that was removed from it.
I'm not completely new, but I'm not advanced in JS either. However, I'm new to algorithms. I've watched Youtube videos for assistance with Queues. I have this concept fairly understood as follows:
//array as queue (FIFO)
const arr = [];
arr.push(5); // [5]
arr.push(7); // [5,7]
arr.push(9); // [5,7,9]
console.log(arr.shift()); // [7,9]
console.log(arr.shift()); // [9]
console.log(arr.shift()); // []
However, when trying to implement this idea into my code, I seem to not maybe fully understand the presented problem in order to make it run. I am to modify the function nextInLine, and the comment that says //Display Code, I am to change that line as well.
function nextInLine(arr, item) {
// Your code here
arr.push(arr[0]);
arr.shift();
return console.log(arr, 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)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
When running the code, this is what is showing for me.
nextInLine([], 5) should return a number.
nextInLine([], 1) should return 1
nextInLine([2], 1) should return 2
nextInLine([5,6,7,8,9], 1) should return 5
After nextInLine(testArr, 10), testArr[4] should be 10
My function is outputting /* 2,3,4,5,1 */ but it seems to be incorrect. Perhaps I am failing to put it into an array? I have the first number (1) at the beginning, but am I failing to simply put my answer in an array? Or am I not even producing the right numbers?