Help with Data Structure Challenge

Hey all.

The JavaScript challenge I am currently attempting instructs me to “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 array. The nextInLine function should then return the element that was removed.”

Here is the code I have so far:

function nextInLine(arr, item) {
  // Your code here
  var nextInLine = (testArr['[]'], 1);
  var addToNextInLine = nextInLine.push();
  var removedFromNextInLine = nextInLine.shift()
  return nextInLine; 

// Test Setup
var testArr = [1,2,3,4,5];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 1)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

Here are the conditions I must satisfy in order to successfully complete this challenge:

  • 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

I somehow managed to complete the first one successfully, even though I got a TypeError message that read “nextInLine.push is not a function”. I am also struggling to complete the remaining three conditions. Because I am asked to “add” and “remove” elements from the array, I took it to mean that push and shift functions were necessary to successfully complete this exercise.

But after running my code and getting that TypeError message, it seems to me that either I wrote the code for the push and shift functions is incorrect or those functions may not be necessary to complete this challenge. So, that being said, my question to all of you is did I write something in my code incorrectly, should I remove something from my code, or am I missing something else?

Please let me know if there is any additional information you would like to know in order to better understand my problem. Thank you to all those who responded to this post and I greatly appreciate your input!

  • Abhi

What are you expecting this line to do: var nextInLine = (testArr['[]'], 1);?

I defined that variable with the initial expectation that the item (in this case, 1) would be added to the end of the array whenever I clicked enter. The number enclosed in the brackets for testArr would be the number that was removed.

I ran the code again, but this time I commented out the push and shift functions. I was able to complete the first condition as a result.