I think I might have the first part right. ```
function nextInLine(arr, item) {
// Your code here
arr.push(item);
item.shift();
return item; // 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));
```
var testArr = [1,2,3,4,5];
console.log(nextInLine(testArr, 6)); // prints 1, what the first element was
console.log(testArr); // testArr is now [2,3,4,5,6]
shift
is an array method that removes the first element and returns it
testArr
to the function nextInLine
(such as nextInLine(testArr, 6)
), the variable arr
inside the function represents the same array as the variable testArr
. Inside the function they only want you to change that array that the variable arr
represents so that any array can be used as input (when calling nextInLine
with testArr
as the input, it will change the variable testArr
as well).
[1,2,3,4,5]
might reside somewhere inside your computer's memory, and the variable testArr
simply points to the memory. Reassigning the variable (such as testArr = "something_else"
) is like pointing to different part of memory, but the actual array might still reside in memory, maybe with another variable pointing to it.var arrayHolderOne = [1,2,3,4];
var arrayHolderTwo = null;
arrayHolderTwo = arrayHolderOne;
// arrayHolderTwo now points to the same array as arrayHolderOne
arrayHolderOne.push(5); // this is adding 5 to the array that arrayHolderOne is pointing to.
console.log(arrayHolderOne); // <-- [1,2,3,4,5]
// even though I didn't directly manipulate the variable arrayHolderTwo,
// the array that arrayHolderTwo points to is the same as a arrayHolderOne
// therefore arrayHolderTwo also equals [1,2,3,4,5]
console.log(arrayHolderTwo); // <- [1,2,3,4,5]
var arr = testArr;
or var arr = [1,2,3,4,5];
But there isn't. Its like they put arr
in there, but it's not assigned to anything. They want me to "remove the first element of array", so I would think that code would have to look like this: ```