Stand in Line - JS Question

Hi guys,

I’ve been staring at this question for a while but finally managed to make it work… I got all the green ticks, allowing me to progress to the next stage, but I don’t think my code is right because the “After” array is the same as the “Before” array, i.e. it hasn’t added the item on to the end?

function nextInLine(arr, item) {
  // Your code here

arr.push(item); // pushes the item on to the end of the array
var next = arr.shift([0]);  // makes next equal to the item removed from the array..
  return next;  // Change this line..
}

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

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

Hello there!

If your code is exactly as what you have posted, you are getting the same returns from the two console.log() because testArr is outside of the function and never passed to the function. This should fix it:

console.log("Before: " + testArr);
console.log(nextInLine(testArr, 1)); // Modify this line to test 
console.log("After: " + JSON.stringify(testArr)); // Returns "After: [2, 3, 4, 5, 1]"

I hope that helps! :slight_smile:

1 Like

That helps! I was confused though because the console code is there by default, written by FCC, so it should have been correct… oh well.

Ah, I didn’t realise the code is there to begin with. I just checked the challenge and the default code is:

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

I think you may have changed it at some point to test things. :slight_smile: