Stand In Line javaScript Exercise ! Stuck!

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.

Thanks !
Zigan

12 Likes

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.

Hope that helps!

6 Likes

@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.

11 Likes

eeflores, ChadKreutzer thanks for you quick reply.

This is what I’ve been doing with your help but still, what am I missing ?

2 Likes

Try push() and shift()

1 Like

I did what you said but didn’t work fredgyoung… :cry:

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.

7 Likes

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.

8 Likes

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

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.

67 Likes

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…

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.

20 Likes

ChadKreutzer thank you with your help I’ve been understanding better what “return” statement is for. :slight_smile:

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

function nextInLine(arr, item) {

arr.push(item);
arr.shift()
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?

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:

function bob(){

}

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

function bob(){
  var foo = 3 * 2
}

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.

function bob(){
  var foo = 3 * 2
  return foo
}

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:

var bar = bob()

is identical to

var bar = 3 * 2

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:

function bob(foo){
  return 3 * foo
}

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:

bob(2)

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:

/* say we pass in [1, 2, 3 ,4] as arr and 5 as item */
function nextInLine(arr, item) { 
  arr.push(item); // at this line, arr will equal [1, 2, 3, 4, 5]
  arr.shift(); // at this line, arr equals [2, 3, 4, 5]
  return arr.shift(); // at this line, arr equals [3, 4, 5]
  /* what Array.shift() does is chop off the value of the first item and
     'return' that value. So this second calling of it on arr returns a
     a value of '2' instead of the '1' that the challenge is expecting.
  */ 
} 
77 Likes

Thanks so much! Your explanation was perfect :smiley:
It’s nice to have someone so helpful and happy to help :relieved:

3 Likes

No problem. I enjoy helping people.

26 Likes

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?

Thanks!

html
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));

4 Likes

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

5 Likes

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?

//PREVIOUS:
function nextInLine(arr, item) {
arr.push(item);
arr.shift();
return arr;
}

// VERSUS CURRENT:
function nextInLine(arr, item) {
arr.push(item);
return arr.shift();
}

6 Likes