Basically, I need to know how to move an array through a queue so that "1' gets knocked off and "6" gets added. I already know to do this I need to use the .push() and .shift() elements. I, however, am stumped on how to do so. Particularly how to structure the code and what to tell the .push() and .shift() to look at. Ex. "blank.push();". The farthest I got was to include "arr.push()" and "arr.shift()" within the function but all that did was knock off the "1". I also need to know why and what I need to change the "return item;" line to. Below is the code I was provided.
function nextInLine(arr, item) {
// Your code here
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));
If all you want to do is provide a solution that works, I should be able to follow the bread crumbs of what goes where and figure out how and why it works but I would very much appreciate a detailed as possible explanation of what is happening in this code for everything above the "//Display Code" line. I have some working knowledge of JS but the less you assume I know the happier I will be. :)
BONUS ROUND: Just curious but if some could tell me why "arr.shift()" is acceptable code (regardless of it's function) but "item.push()" or "item.shift()" errors as "TypeError: item.push is not a function." To me, they are both just similar parameters of this function. Why does one error out while the other doesn't? Is there a programmed meaning to "arr" or trait I am unaware of?