I'm currently engaged in learning Javascript through freecodecamp and am diving into the topic of functions.
Currently, I'm tackling a problem where I need to create a type of queue
that can remove the first item from an array and replace it with another item at the end of the array.
Here's my initial attempt:
function nextInLine(arr, item) {
// Your code here
array = [];
array.shift(arr);
array.push(item);
return arr; // 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));
However, when I run this with the provided test setup, the output is as follows:
Before: [1, 2, 3, 4, 5]
After: [1, 2, 3, 4, 5]
I'm quite perplexed and unsure how to move forward. How can I successfully complete this task?
The main objective is as follows:
In the realm of Computer Science, a queue is considered an abstract Data Structure that maintains items in order. New items are added to the back of the queue while old items are removed from the front of the queue.
Your goal is to write a function called nextInLine that requires an array (arr) and a number (item) as arguments. The function should add the number to the end of the array, then remove the first element of the array. Finally, the nextInLine function should return the element that was removed.