## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
David Castner
@davidjcastner
Hey Matt, how's it going?
Matt Richards
@mattjrichards
It's going alright, I guess. Having trouble with Lesson #173.
David Castner
@davidjcastner
what is lesson #173?
Matt Richards
@mattjrichards
I think it's the way they have the directions worded. 3 other people were also having trouble with the same problem. The name of the lesson is "Stand in Line."
Here is part of the instructions: 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.
David Castner
@davidjcastner
okay so it's asking you to use the array as a queue, do you understand the concept of a queue?
Matt Richards
@mattjrichards
That is the section that I'm having trouble understanding. They don't elaborate enough on what they want you to do, given what I've learned in the previous lessons.
yes

I think I might have the first part right. 
function nextInLine(arr, item) {
arr.push(item);
item.shift();
return item; // 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));


David Castner
@davidjcastner
Okay it's asking you to take the input item and add it to the end of the array, then remove the first element (shifting all other element as well) and returning the first element (what was the first element)
Matt Richards
@mattjrichards
That's what I don't get. What is the first element?
David Castner
@davidjcastner
for their test array, 1 is the first element
for example
var testArr = [1,2,3,4,5];
console.log(nextInLine(testArr, 6)); // prints 1, what the first element was
console.log(testArr); // testArr is now [2,3,4,5,6]
Hint: shift is an array method that removes the first element and returns it
David Castner
@davidjcastner
let me know if you're still having trouble
Matt Richards
@mattjrichards
sorry buddy, I'm just slow
David Castner
@davidjcastner
haha no worries, is there something in particular that you are still not understanding?
Matt Richards
@mattjrichards
I'm trying to figure out how to ask my question so that it makes sense to you, but I'm having trouble finding the words.
David Castner
@davidjcastner
alright i'll be back in a moment
Matt Richards
@mattjrichards
ok
Matt Richards
@mattjrichards
In this sentence, "Add the number to the end of the array, then remove the first element of array.", when they say "array" are they referring to var testArr = [1,2,3,4,5]; or are they referring to arr in function nextInLine(arr, item) { ?
David Castner
@davidjcastner
ahh okay, they are referring to both. When you pass testArr to the function nextInLine (such as nextInLine(testArr, 6)), the variable arr inside the function represents the same array as the variable testArr. Inside the function they only want you to change that array that the variable arr represents so that any array can be used as input (when calling nextInLine with testArr as the input, it will change the variable testArr as well).
Matt Richards
@mattjrichards
Ok. Let me let that sink in for a bit. I'm trying to visualize all this in my head, but arr and testArr are tripping me up.
David Castner
@davidjcastner
It is important to think of variables are references to objects, but not the actual object. For example, the array [1,2,3,4,5] might reside somewhere inside your computer's memory, and the variable testArr simply points to the memory. Reassigning the variable (such as testArr = "something_else") is like pointing to different part of memory, but the actual array might still reside in memory, maybe with another variable pointing to it.
var arrayHolderOne = [1,2,3,4];
var arrayHolderTwo = null;
arrayHolderTwo = arrayHolderOne;
// arrayHolderTwo now points to the same array as arrayHolderOne

arrayHolderOne.push(5); // this is adding 5 to the array that arrayHolderOne is pointing to.
console.log(arrayHolderOne); // <-- [1,2,3,4,5]
// even though I didn't directly manipulate the variable arrayHolderTwo,
// the array that arrayHolderTwo points to is the same as a arrayHolderOne
// therefore arrayHolderTwo also equals [1,2,3,4,5]
console.log(arrayHolderTwo); // <- [1,2,3,4,5]
Lily Romano
@Lilyheart
Man, I go out to grab dinner cuz I'm having a bad brain day and come back to so much chatter! :heartpulse:
imma look at that code now David.
Matt Richards
@mattjrichards
Ok. I'm still a bit confused. If "arr" and "testArr" are the same thing, or point to the same thing, I would think there would have to be a line like this somewhere in the code: var arr = testArr; or var arr = [1,2,3,4,5];But there isn't. Its like they put arr in there, but it's not assigned to anything. They want me to "remove the first element of array", so I would think that code would have to look like this: 
var arr = [1,2,3,4,5];
hold on. i wasn't finished
i don't know how to format this
Lily Romano
@Lilyheart
the first  needs to be on it's own line. You can go back and edit it.
David Castner
@davidjcastner
it's okay. I'll put together something for a better explanation using the problem as an example
Jonathan Herman
@lantech19446
that was a really difficult concept for me also
Matt Richards
@mattjrichards
var arr = [1,2,3,4,5];
removedFromOurArray = arr.shift();
Hi @Lilyheart and @lantech19446
Lily Romano
@Lilyheart
After those two lines arr would = [2,3,4,5] and removedFromOurArray would = 1
Jonathan Herman
@lantech19446
hey @kilocycle how goes it
Matt Richards
@mattjrichards
ehhh...this lesson is extremely confusing.
David Castner
@davidjcastner
one moment, almost done
Matt Richards
@mattjrichards
Lily Romano
@Lilyheart
Imma wait until David is done before I chime in. He does these incredible writeups usually before I can get many words typed up xD :heart_decoration:
Matt Richards
@mattjrichards
this is my 3rd night on this lesson. We got into it heavy last night in fcc chat.
there were 2 other people stuck on the same lesson, having the same issues.
it's like you're cruising through the lessons at a good pace, then you get to #173 and it's a brick wall.
Lily Romano
@Lilyheart
data structures can be challenging. New concept for many.
Matt Richards
@mattjrichards
Yes, it is very challenging for me to say the least.
David Castner
@davidjcastner
alright hopefully this real world example helps:
Matt Richards
@mattjrichards
@davidjcastner ok
David Castner
@davidjcastner
/*
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.
*/

/*
To make this easier to understand let's equate it to a real world example of
standing in line at the cash register. The cashier is going to ask for the next
customer in line, and a another customer in the store is going to get in line
for the register.
*/

/*
Now suppose the coffe shop, StarElks, has two registers and two lines for each
register. Each line alreays has some customers in it.

Let's represent those customers in their line with some arrays.
*/
var customersForRegisterA = ["alice", "bob", "carol"];
var customersForRegisterB = ["danny", "eve"];

/*
There are two cashiers who are overly anal retentive, Fred & Ginny.
Both Fred and Ginny are not adapt at social interacting with people.
They also cannot live life happily unless the amount of customers in their
lines remains the same. Therefore, when a new customer enters a line,
Fred or Ginny finishes the transaction of their current customer. If no one
enters their line, they stall their current customer with awkward silence.

Let's say that Fred is operating register A, and Ginny is operating register B.
Both Frad & Ginny currently have a customer.
Let's store those customers as well.
*/
var fredsCustomer = "hailey";
var ginnysCustomer = "isaac";

/*
The manager of StarElks realizes that Fred and Ginny are too afraid to talk to
the customers, so the manage decides to get a javascript program to help manage
the lines for the registers.

Let's call this program (and function), nextInLine.
*/
function nextInLine(lineForRegister, newCustomer) {
// the code here should manipulate the lineForRegister by
// adding the newCustomer and returning who is next
// Hint: do not use the variables customersForRegisterA and customersForRegisterB
var nextCustomer;
// code to manipulate lineForRegister
return nextCustomer;
}

/*
A new customer, Jill, enters the store and gets in line for register A.
Fred realizes this, so he knows he can finish Hailey's transaction.
Fred isn't sure who his next customer is, but he can use the new javascript
program to help.

Prior to this, Fred's customer is Hailey and he has Alice, Bob, and Carol in
line for his register.
*/
var fredsCustomer = nextInLine(customersForRegisterA, "jill");
// Hailey left the store, so Fred's customer is now Alice.
console.log(fredsCustomer); // alice
// Alice is no longer in line, but Jill entered the line.
console.log(customersForRegisterA); // ["bob", "carol", "jill"]

/*
Because the program takes the value that the variables point to, it can be used
for Ginny's line as well.

Another customer, Kevin, enters StarElks. Kevin gets in lien for register B
(Ginny's register).

Prior to this, Ginny's customer is Isaac and she has Danny and Eve in line for
her register.
*/
var ginnysCustomer = nextInLine(customersForRegisterB, "kevin");
// Isaac left the store, so Ginny's customer is now Danny.
console.log(ginnysCustomer); // danny
// Danny is no longer in line, but Kevin entered the line.
console.log(customersForRegisterB); // ["eve", "kevin"]
hopefully that's easier to read now
Matt Richards
@mattjrichards
yes. thanx for word wrap...lol