Stand In Line Challenge: Any Help Please?

Stand In Line Challenge: Any Help Please?
0

#1

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/


#2

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
}

#3

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?


#4

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.


#5

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


#6

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.


#7

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?


#8

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.


#9

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


#10

Now you understand what is going on.


#11

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?


#12

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


#13

Like this

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

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

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


#14

Sure.

var elemRemoved = arr.shift();
return elemRemoved;

But it is unnecessary and less concise code to to this. The only reason you would assign the shift result to another variable, is if you needed to reference it somewhere before the return statement. But since you are not, you do not need the extra line of code.


#15

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