Return statement with array and argument

Tell us what’s happening:
Describe your issue in detail here.

Instruction:

nextInLine([], 5) should return a number.
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

So I passed the test using the help of video but I don’t understand where nextInLine([5,6,7,8,9],1) came from and why it return 5?

Thanks for help

Your code so far


function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);

return arr.shift([]);
// Only change code above this line
}

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

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));
  **Your browser information:**

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

Challenge: Stand in Line

Link to the challenge:

You add the given value to the end of the array using push then remove the first value in the array using shift, then return that value that has been shifted off.

The starting array is [5,6,7,8,9].
If you add 1 to the end of the array, it is now [5,6,7,8,9,1].
If you remove the first value from the array, the array is now [6,7,8,9,1]
The first value is 5.


shift doesn’t take any arguments. It’s just a command: “running the function shift() on an array will remove the first value from the array and return it”.

The code still works because JavaScript functions can have any number of arguments: if they aren’t used, JavaScript will ignore them.

But it should just be shift()

I get the function of the shift and push but I don’t understand where [5, 6, 7,8, 9, 1] came from. When I run the test, I cannot see any of those number and the test still passes.

// running tests
// tests completed
// console output
Before: [1,2,3,4,5]
1
After: [2,3,4,5,6]
Before: [1,2,3,4,5]
1
After: [2,3,4,5,6]

That’s an array that the tests are passing into your function. Do a console.log of arr at the beginning of the function and you’ll see it.

Where should I put a console.log of arr in the scripter to see the code?

Your code so far


function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);

return arr.shift([]);
// Only change code above this line
}

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

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

You want to see what the value of arr is when it is passed into the function before you modify it. Based on that, where do you think you should put it?

Is it before the push and shift command?

function nextInLine(arr, item) {
  // Only change code below this line
  console.log(arr);
  arr.push(item);

  return arr.shift([]);
  // Only change code above this line
}

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

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.