nextInLine([testArr], 10),testArr[4]); Laymen's terms?

Tell us what’s happening:
I added comments to my function, so I could follow what I was doing. However, I’m 100% sure that I do not understand what this line is doing. Can anyone explain this in laymen’s terms? I think I’m getting caught up in my confusion and I need help.

nextInLine([testArr], 10),testArr[4]);

Your code so far


function nextInLine(arr, item) {
  // Your code here
  arr.push(item); //adds item to the end of arr
  var removed=arr.shift(); //removes first element of arr
  return removed;  // Change this line
}

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

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

//What is line 13 doing?!? 

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line/

1 Like

If I understand what you’re saying, this line is the one that has you confused. So let’s see if we can clarify a little.

First, the console.log() is simply going to display whatever values you ask it. If you provide a string, it’ll display a string. If you provide a boolean (or an expression that evaluates to a boolean), it’ll say true or false. If you provide it a function, it will display the return value from that function. And that’s what happens here.

The first part of the console.log(), the nextInLine([testArr], 10), contains a small typo – testArr is already an array, so you’re passing the function an array with an array INSIDE it - i think that may be confusing things a little. simply pass console.log(nextInLine(testArr, 10); to see what that function is returning. As you have the last line of the function as return removed;, then that’s what you should see in the console.

When that function runs, it pulls the FIRST member off the array, and pushes a new one onto the LAST position. So the length remains the same, and testArr[4] always points to the fifth member of that array (remember, zero-based index). That should show the member you passed into that function, as the new last member.

So, console.log(nextInLine(testArr, 10), testArr[4]); should output 1, 10 when it is run against this code. The 1 is from the nextInLine() return statement: that pulled the first member out of the array and returned it. At the same time, nextInLine() added the passed value to the END of the array, so the 10 in the displayed line is the new last member of our testArr.

Hope that helps.