Tell us what’s happening:
Hi all,
I’ve searched the forum for an answer on this, but to no avail.
After a fair amount of searching, I was able to reach the end of the challenge, but there is a part I don’t understand in two of the examples at the end:
In the highlighted areas, I don’t understand why it does’t return a blank result, based on the following:
When I run console.log, I am applying values of and 5. My understanding of this is that the array should result in a blank value at this stage.
-
The first step in the function appends ‘arr’ with ‘item’, so, we should end up with blank + 5.
-
The second step removes the first element of the array, and assigns it to a variable called ‘removed’, so now I have ‘removed’ = blank.
-
The closing step states that the removed variable is what should be returned when the function is activated (that’s my understandig of it at this early stage in my JavaScript journey).
So, if I am applying value of blank and 5 to the arguments, why does this not return a value of blank? I don’t understand why it returns a value of 5, as I see it like this:
blank + 5 = blank, 5
var removed = blank
return removed
If removed = blank, why does it return a value of 5? Is it as simple as JavaScript realising that the result is blank, so it then defaults to the next item in the list?
Your code so far
function nextInLine(arr, item) {
// Your code here
arr.push(item);
var removed = arr.shift();
return removed; // Change this line
}
console.log(nextInLine([], 5));
// 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36
.
Link to the challenge: