[null, {"document_url": "https://www.freecodecamp.org/forum/t/stand-in-line-javascript-exercise-stuck/5103", "unformatted_src": "//discourse-user-assets.s3.amazonaws.com/optimized/2X/a/aeb54374dbf808730e439b53cf2d07f81a5fe823_1_690x139.PNG", "src": "http://discourse-user-assets.s3.amazonaws.com/optimized/2X/a/aeb54374dbf808730e439b53cf2d07f81a5fe823_1_690x139.PNG", "formatted_filename": "aeb dbf e b cf d f a fe", "rendered_width": 690, "rendered_height": 139}, null, {"document_url": "https://www.freecodecamp.org/forum/t/stand-in-line-javascript-exercise-stuck/5103", "unformatted_src": "//discourse-user-assets.s3.amazonaws.com/original/2X/c/c00ab1ce39a36c247bdfee6cfc5eb8412e020eaf.PNG", "src": "http://discourse-user-assets.s3.amazonaws.com/original/2X/c/c00ab1ce39a36c247bdfee6cfc5eb8412e020eaf.PNG", "formatted_filename": "c ab ce a c bdfee cfc eb e eaf", "rendered_width": 642, "rendered_height": 328}, null, null, {"document_url": "https://www.freecodecamp.org/forum/t/stand-in-line-javascript-exercise-stuck/5103", "unformatted_src": "//discourse-user-assets.s3.amazonaws.com/optimized/2X/2/22e2359abc5d46528956f57d72fa8c85e743673d_1_391x500.png", "src": "http://discourse-user-assets.s3.amazonaws.com/optimized/2X/2/22e2359abc5d46528956f57d72fa8c85e743673d_1_391x500.png", "formatted_filename": "e abc d f d fa c e d", "rendered_width": 391, "rendered_height": 500}, null, null, null, null]
Stand In Line javaScript Exercise ! Stuck!

Zigantheshaman 2016-05-27 04:59:27 UTC #1

Hi guys I’ve been stuck here for several days trying to find a solution by myself but I still don’t get what I have to do.

Stand in Line

In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.Write a function nextInLine which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The nextInLine function should then return the element that was removed. Capture.PNG1375x278 9.15 KB


eeflores 2016-05-27 05:07:35 UTC #2

Hi Zigan,

the nextInLine() function should be modified so that the arr parameter (which is an array) is treated as a queue. the arr parameter needs to be modified so that the item parameter to the function is added to the end of arr, and the first element of arr is removed. The element that is removed from arr should be returned from the function.

Many solutions to this exercise use array functions - you can google “mdn array shift” and “mdn array push” - the shift function can be used to remove the first element from arr, which in turn can be used as a return value in the nextInLine function. The push function can be used to add an element to the end of an array.

One more tip: add the item element to the end of the array arr before removing the first element from the array arr.


ChadKreutzer 2016-05-27 05:48:09 UTC #3

@eeflores is correct, but let’s see if I can put it more simply.
Say you have an array:
var bob = [1, 2, 3, 4, 5]
and a number:
var jones = 6

Using push(x) on bob will tack jones onto the end of it and return the new length of 6:
bob.push(jones) === bob.length && bob.length === 6 will return true, after which:
console.log(bob) will print [1, 2, 3, 4, 5, 6] to the console.

Using shift() on bob will take the 1 off of it and return that 1:
bob.shift() === 1 && bob.length === 4 will return true and:
console.log(bob) will print [2, 3, 4, 5] to the console.

so for this challenge, you need to combine those two methods.

Zigantheshaman 2016-05-27 06:56:30 UTC #4

eeflores, ChadKreutzer thanks for you quick reply.

This is what I’ve been doing with your help but still, what am I missing ? fredgyoung 2016-05-27 07:03:47 UTC #5

Try push() and shift()

Zigantheshaman 2016-05-27 07:09:32 UTC #6

I did what you said but didn’t work fredgyoung… ChadKreutzer 2016-05-27 07:12:10 UTC #7

First: testArr is what is being passed into your function as arr. You don’t want to operate directly on it. You want to operate on arr

Second. Since item is what is passed into your function along with arr, you can operate directly on it like you can with arr.

Third, what you are wanting to return is not item, but the results of arr.shift().

Hopefully that helps. Let us know if you are still stuck.

ChadKreutzer 2016-05-27 07:43:58 UTC #8

Let me see if I can blow your mind: you only need two lines to solve this challenge.

Remember, we don’t care about the value push() returns, all we care about is what it does to arr.

Conversely, we don’t care about what shift() does to arr, all we care about is the value it returns.

Zigantheshaman 2016-05-28 07:55:09 UTC #9 2016-05-28 17.53.19.png939x1198 134 KB

This what I did bust still I don’t get exactly what you asked me to do…

ChadKreutzer 2016-05-28 08:04:04 UTC #10

Close. Change that 6 to item. And remember we’re looking to return what shift did to arr, not arr itself. So try returning arr.shift(). As soon as I get back to a computer, I’ll explain better. (It’s a pain typing on my phone)

Okay. So Array.push(x) and Array.shift() are what I call dual wielding methods. they return a value on the “left hand” : ( var y = Array.push(x) ) and since they change the object they are working on (the Array), they also return something on the “right hand”: (the new value of Array) In this challenge, we are going to take advantage of that property.

first you take the array arr and use .push(x) on it: arr.push(item). Using the left hand/right hand analogy the result of this is:

the new length of arr <--- arr.push(item) ---> the actual arr array with item added to the end (it was pushed on to arr. get it?)

we don’t care about the new length of arr, but we do want to use the changed value of arr. that’s why we don’t bother assigning a variable here.

so now we have our shiny new arr. What we want to get is the value at the 0 index (arr[0]) but we can’t just say return arr[0] because the validator is going to check to make sure that the length of arr decreases by one.

In order to do that, we use arr.shift(). continuing with the right hand/left hand:

the contents of the zero index of arr <--- arr.shift() ---> the actual arr array minus it's old zero index item (everything shifted to the left. get it?)

now since what we want is the left hand result, we could do a variable assignment then return that, or we can just return it directly: return arr.shift().

That’s a whole lot of words for a two line function, but hopefully I was able to make it clear what is going on.

Zigantheshaman 2016-05-29 15:18:54 UTC #11

ChadKreutzer thank you very much for your help and I did what you told me.
It worked like a charm!

I understood what arr.push(item) does to the Array but I still don’t get what the “return” function is for…

ChadKreutzer 2016-05-29 18:27:00 UTC #12

Technically return is a statement. The things that end in “()” are functions (or methods) if you look at the top of all the challenges, what you are creating are functions. In this case, it’s the function nextInLine(). Functions are like little boxes that do something. Remember my hand analogy? A return statement is what tells a function to deliver a left hand result: you are telling it to “return” the value of whatever is directly to its right out of the function. It’s like where the copies come out on a copier.

It’s essentially saying stop what you are doing and give that to me. In fact, in the arr.push(item) function, the new length of arr that you get on the left hand is the result of a return this.length statement.

Zigantheshaman 2016-06-02 05:10:20 UTC #13

ChadKreutzer thank you with your help I’ve been understanding better what “return” statement is for. Stoarme 2016-06-13 08:31:37 UTC #14

Hi, I was stuck on this as well for a while, and I was wondering why you don’t do:

function nextInLine(arr, item) {

return arr.shift();

Maybe it’s because I don’t fully understand the purpose of return?
With return, does it also always execute what you tell it to give?

ChadKreutzer 2016-06-15 07:04:58 UTC #15

return does always tell the function “We’re done, spit out the value of the thing to my right.” Think of a function like a little mechanical box that you can put things into and get things out of. Say we have a function called bob:

right now, this function is an empty box. it doesn’t do anything. but lets put something inside it, ok?

now bob is doing something. It is assigning the variable foo the value of 3*2, but if you called this function, it would still do nothing. We need some way to get the value of foo out of bob. that’s where return comes in.

Now we’re getting somewhere! return tells bob to return foo back to us. so at this point, if you called bob, it would be just like calling foo:

is identical to

That’s kinda silly, but there are times where maybe you want to hide a complex calculation that always give the same result. and maybe you want to be able to change how the program reaches that result, but you only want to have to do it in one place. Where functions real power comes in is when you put in a value to get out a different value. But you need return on the other end to get anything out:

Now we’re cooking with mesquite! Think of those parentheses after the function name as the opening at the top of the box: whatever you put there is going to go inside. The function will do its magic, and then with the return statement, it spits back out whatever you want it to spit out.

so calling bob:

will give you 6. But remember, without the return, you will just be sending the 2 in to languish forever.

There are other ways around this structure, but they are usually code smell, and you want to avoid them when possible. Hopefully that will clear up any misunderstanding you have of the return statement.

The problem we have with your example is that you are calling Array.shift() twice. each time you call it, it chops the first value off of the array that you are calling it on:

Stoarme 2016-06-15 07:50:39 UTC #16

Thanks so much! Your explanation was perfect It’s nice to have someone so helpful and happy to help ChadKreutzer 2016-06-15 07:53:56 UTC #17

No problem. I enjoy helping people.

Sadaajit 2016-07-02 20:04:26 UTC #18

I’ve read through this whole thread and still don’t have working code. I must be missing something in the translation of your instructions. Could you please take a look at what I’ve got and help me understand what I’m doing wrong?


ChadKreutzer 2016-07-03 00:38:09 UTC #19

It’s the extra arr.shift(). Each time you call that, it does its thing.

BrennaBaccaro 2016-07-04 05:54:28 UTC #20

EDIT: I got it to work. Interesting, not sure why my previous code is so wrong, though. Is it because in the previous code my “return arr” is returning the array inside the function…so it’s getting returned twice when the function is called? Anyone care to explain the difference in meaning between previous and current?

return arr;

