0

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?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Austin
  • 1
  • Is this similar to what you are asking? http://stackoverflow.com/questions/1590247/how-do-you-implement-a-stack-and-a-queue-in-javascript – Chris Jan 13 '17 at 01:09
  • Possible duplicate of [How do you implement a Stack and a Queue in JavaScript?](http://stackoverflow.com/questions/1590247/how-do-you-implement-a-stack-and-a-queue-in-javascript) – Chris Jan 13 '17 at 01:10
  • i should add that you really need to look a bit at the documentation for the functions you use, it would have spared you time ;) – Kaddath Jan 13 '17 at 01:12
  • 1
    `shift` and `push` are array methods. `item` has the value `6` in your code. `6` is a number, not an array, and doesn't have these methods. – melpomene Jan 13 '17 at 01:14

2 Answers2

1
function nextInLine(arr, item) {
    var nextItem = arr.shift(); // This removes the first element of the array, being 1, and assigns it to the nextItem variable.
    // Now arr = [2,3,4,5]
    arr.push(item); // This adds the item 6 to the end of the queue
    // Now arr = [2,3,4,5,6]
    return nextItem;
}

Bonus Answer:
arr in this context is an array, which has push and shift methods.
and item is a number, which doesn't have such methods

GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19
0
function nextInLine(arr, item) {
    // Your code here
    arr.shift();
    arr.push(item);
    return item;  // Change this line
}
Mathew Berg
  • 28,625
  • 11
  • 69
  • 90