Stand in Line help understanding the code!

Stand in Line help understanding the code!
0

#1

Tell us what’s happening:
I understand that I have the problem correct, but the issue is that I’m having is understanding WHY it is right and understanding it before I continue forward. can someone please explain this to me? this is what I think I understand from it:

1.“arr” and “item” are placeholders for the function “nextInLine”.

2.“arr.push (item)” this puts “item” to the end of the line (but how can you .push() a placeholder?). I believe in this case it will take the first number (1) and put it to the end of the line so that the array will look like “[2,3,4,5,1]”

3.create a variable named “removed” that will hold the value of “arr.shift()” the number at the end in this case the number “1”.
4. return “removed” as the new function parameter so it would look like this “function nextInLine(removed)”

i feel that there is something very crucial that I am not understanding in this… what am I missing? or not understanding

thank you!
Christian

Your code so far

function nextInLine(arr, item) {
  
  arr.push(item); // placing item at the end of arr
  var removed = arr.shift(); //giving removed the value of "arr.shift()"= ??
  return removed;  
}

// 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 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0.

Link to the challenge:


#2
  1. arr and item are arguments for the nextInLine function. They are variables which have been passed into the function when the function is called.

  2. You can use .push(item), because arr is an array. push is a method which adds an element to the end of an array. In this test case of nextInLine(testArr, 6), testArr is passed to the arr argument and the number 6 is passed to item. So instead of 1 being pushed onto the end of arr, the number 6 is pushed onto the end of arr. After the arr.push(item), arr looks like: [1,2,3,4,5,6]

  3. shift() removes the first element of an array and returns is value. In this case, the number 1 is removed and returned, so it gets assigned to the variable removed.

  4. The return removed returns the removed variable value back where the function was originally called. In this case:
    console.log(nextInLine(testArr, 6)); becomes console.log(1); so 1 is displayed to the console.


#3

ok great thank you, after taking the night off and relaxing my brain I think i understand this a lot better, plus some different ways of explaining the same thing always help also.

thanks for the reply