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
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.
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.
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?