Stand In Line Challenge: Any Help Please?

I have looked at the other answers to the people who had the same problem as me. I know that I am doing the pushing the number part right, but what I don’t get is why when I am removing and returning the first element is the code not working. If anyone could help me with the conceptual understanding of this topic, it would be immensely appreciated.

Your code so far


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

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36.

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

Your code is nearly OK apart from one error.

function nextInLine(arr, item) {
  // Your code here
  arr.push(item); // add item to Array
  arr.shift(); // remove first item
  return arr.shift();  // You remove the first item again so the returned number with the first `testArray` will be `2` as you already removed `1` when you called the shift() method first
}

But since I have the return in front of the arr.shift(); wouldn’t it then tell the computer to return whatever element it was that had been removed in the first place?

No, it will use the method and return. It is a bit confusing but this is how it works.

There is a good example

return a > b ? true : false;

What this ternary operator does? First check the condition and THEN returns true or false.

So in your case first run the method and returned.

Sorry I still don’t understand. I get the condition part but I fail to see how this applies to my situation.

Okay I try to use your case.

  1. you add an elem to the end of the Array
  2. you remove an elem from the Array
  3. you remove an elem from the Array and return that one.

Your Array looks like this

Base [1, 2, 3, 4, 5];
After push - [ 1, 2, 3, 4, 5, 6];
After shift - [ 2, 3, 4, 5, 6];
When you return you shift again so your array will be - [3, 4, 5, 6]; and return 2

If you use a method or checking for condition, they will be executed first and then returned.

Sorry I can not explain it better.

I think I understand what you mean. What I don’t understand is why the return arr.shift(); is removing another element from the beginning of the array rather than returning the element back?

You are shifting two times instead of just one time. Whenever you write arr.shift() it is removing the first element from arr. In the return statement it not only removes an element, but it returns the element which was removed.

So the return line does both things at once; it removes the element and then returns it simultaneously?

I have two questions:

  1. So if including arr.shift(); in the return line is doing both tasks, why would anyone want to do that? Does it have any real-life uses or is this shown to us just for the sake of showing us?

  2. Is there a way so that I can do arr.shift(); and return the element that I shifted, in two lines rather than in one line?

YES, you are right. But first removes then returns.

1 Like

Like this

let shiftedElem = art.shift();
return shiftedElem;

There is real life case. I do all the time if I need to.

Edit as @camperextraordinaire mentioned I would store in variable if I need to use it somewhere else.

I finally understand. Thank you both of you. You helped me greatly.

1 Like