Continuing the discussion from freeCodeCamp Challenge Guide: Stand in Line:
Initially I was able to get 50% of the way to understanding of this script challenge. The concept of adding and removing items to an array and the methods to do it made easy sense and easy work.
After reading forums I was then able to get my understanding to about 75% and complete the challenge, albeit with a gap in my understanding.
However, after re-reading the last of the two steps(I feel kinda stupid) I see now, that in order to return the removed array element, you need to store it somewhere first, i.e. in a variable or just eliminate the variable and encapsulate the removal into “return”.
The
nextInLine
function should then return the element that was removed.
What I would like to try and understand now about this is…“what is the greater learning takeaway here in first storing the removed array item in a variable to then remove it?” wouldn’t it work by just removing the array item using only…
arr.shift() /* or */ arr.shift(item)
// ??
Why is the shifted item from the array getting returned and not the pushed item?