Stand in Line: Challenge on queues

Tell us what’s happening:
How do I get nextInLine to return the other values in the challenge, namely:

  1. nextInLine([], 1) should return 1

  2. nextInLine([2], 1) should return 2

  3. nextInLine([5,6,7,8,9], 1) should return 5

Your code so far


function nextInLine(arr, item) {
  testArr.push(item);    // Add item to end of test arr
  let removed = testArr.shift();   // Remove first element from testArr    
  return removed;  // 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));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line/

There’s a semicolon on the line after return removed.

I removed the semicolon after removed ( matterof fact I rewrote the code block ) and it is still not the right solution, leaving me kind of stumped.

The only other idea I have is to somehow iterate through this function with a loop but not quite sure how I can implement that.

Any idea’s on the implementation or what else I could be missing?

function nextInLine(arr, item) {
  testArr.push(item);  //Add item to end of testArr
  let removed = testArr.shift();// Remove first element from testArr
  return removed;  // 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));type or paste code here

The issue is that your function is not reusable. The function has two parameters, item and arr. Your code works only on testArr, not on any other array that could be passed in the function

@ilenia Thank you that worked and I learned something new about arr and functions.