Stand In Line Discussion

Continuing the discussion from freeCodeCamp Challenge Guide: Stand in Line:

my solution:

arr.push(item);
var item=arr.shift();
return item;

my question is why anyone havn’t use shift() function to add item in the beginning?
i mean we can if not used return statement then can we use this like arr.shift(); ?

the challenge doesn’t ask to add an item at the beginning tho, it asks to add it at the end

1 Like

no, at first we have to add the element in the end and then we have to add the element in the beginning.

you have to add an element at the end and remove an element from the beginning
unshift add an item to the beginning, it can’t be used here

That would be a 'Cut in Line" lesson, rather than a stand in line one.

Think of it as a stack of orders in a diner - orders get added to the end of the list but get handled from the beginning of the list. Otherwise, your first diners in would never get handled, and see everyone who came in after them eating while they’re still waiting!

2 Likes

sorry instead of shift() i had written unshift but my question is still the same we can use that function and for that we don’t even need to a return statement, if not then why?

Sir, I got the question but my question is about using shift() operator i am not understanding ,because when i had first tried to solve this challenge i have used arr.push();
arr.shift();

@Komal-1 I was doing this same challenge this morning, lol. Great example btw @snowmonkey

have you stuck in the same question which i am thinking that it should be used ?

I guess I’m not quite getting the question. Array.push and array.pop are the functions to add to or remove from the end of the array, while array.unshift and array.shift add to and remove from the beginning of the array.

So in this case we push to add to the end, then shift to remove from the beginning.

yes “So in this case we push to add to the end, then shift to remove from the beginning.”
exactly this is what i am saying why there is no use of shift for removing element in the second part of the question.?

No, I didn’t think the problem in that way. That’s why I was following ur post to know opinion of the others :grinning:

But you are. var item = arr.shift() removes that first thing from the array and then returns that removed item, which we assign to the item variable.

2 Likes

shift removes the first element of an array

unshift adds items to the beginning of the array

pop removes the last item of the array

push adds items to the end of the array


There is only one of these that adds an item in the right spot.

There is only one of these that removes an item in the right spot.

see functions are used for reducing code and basically code duplication and make it efficient and when we have already function to remove beginning element then what is the need to assign it item or any other variable ?
suppose this code is small that’s why we easily dont’t thinking about that but what if there is more than one statement in console.log or in that function then ?

2 Likes

Ahhhh and you’re right. We could have avoided the variable and simply return arr.shift() for the exact same result, with the added benefit of not using a 'throwaway variable."

That’s some good thinking, partof a collection of rules referred to as “the elements of javascript style” in some circles.

2 Likes

yes exactly . this is what i am saying

1 Like

Both approaches are valid, but you’re completely correct that finding ways to minimize unnecessary variables is good practice, so kind as your code remains readable.

In This case, i prefer your solution. It explicitly says what it’s returning, rather than having to find what item references somewhere in your code. Short code, but still it is explicit.

1 Like

I imagine that the solution in the guide was written in three steps to show the three parts of the instructions separately.

4 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.