Stand in Line - I don't understand the solution!

Stand in Line - I don't understand the solution!
0.0 0

#1

Tell us what’s happening:
After spending hours on this, I ended up looking at the solution code and don’t understand how it works.

First, how does the function know which string we are referring to? Sure, we declared testArr, but at no point in the function do we specify it.

Second, how is the ‘item’ parameter specified to 6? I don’t see where that value is specified. I spent a ton of time trying to write a line that called the function and passed arguments into the parameters, which it turns out was causing me an error.

I hate admitting defeat by looking at the solution, but now I’m even more confused.

Your code so far

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  return arr.shift();  // 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));

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:


#2

You are passing the array into the function. The methods Array.push() and Array.shift() operate on the values they are giving, changing them, not merely returning the values. Though Array.shift() does return the value removed from the array as well.


#3

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


#4
function nextInLine(arr, item) { //arr = testArr, item = 6
  arr.push(item);   //push 6 onto the right end of array
  return arr.shift();    // take the leftmost element off array and return that
}

// Test Setup
var testArr = [1,2,3,4,5];
console.log(nextInLine(testArr, 6)); // Send testArr and the number 6 to nextInLine function

#5

@randelldawson Red Alert Again! haha.