Stand in line challenge

Okay guys, now look at this picture and please correct me if im mistaken. I solved it with a bit of help, but i dont know if i understand it: If you look at the instructions, it means to push an item… so now we have 1 arr, and two items (arr, item, item), and after that, we have to shift the first element of an array, which makes it ‘‘arr’’, so now we have (item, item), we have two items, and no arrs, is that it?

push adds an item to the end of the array and shift removes one from the front (1st element) I believe.

Read the docs if you are still not sure

My solution to the problem

See this is the correct answer, but I dont get it…What is arr anyway? its a argument aint it? so why cant i put nextInLine,push(), or item.push(), instead of arr.push()… why arr? arr is the same as item to me, so why cant i assign item,push()… This really confuses me, And as it goes for the answer on the left, i have no idea what is going on, and what is ‘‘JSON.stringify’’ anyway… i google it a bit, but i havent done a single order on this site, and it just shows up from nowhere… :S

I think if you ask these questions on the gitter channel you will get answers quicker.
Many questions! https://gitter.im/FreeCodeCamp/Help

Note. JSON.stringify helps present the data better in the output by turning the object to a string. Otherwises it would appear as [object Object]

You pass parameters into a function as arguments and it returns a value.

function divide(arg1, arg2) {
  var myVal = arg1 / arg2;
  return myVal;
}

var param1 = 10;
var param2 = 5;

divide(param1, param2);
// returns 2

Note that parameters can just as easily be arrays, strings, or objects. In your case, the first parameter is an array, and the second is a number.

2 Likes

I’m not much further down the road then you, so maybe I’m wrong. ‘arr’ is an Array, the .push() function can put an ‘item’ in there using array.push(item). Trying to put item.push() is like trying to stuff a turkey with another turkey :grin:. Shoving an entire function in the will get complicated as well, I think, maybe it’s useful down the road.

1 Like

arr just stands for an array? because in my function (arr & item) are just parameters, waiting to be declared

Yes: it could be called djcbrdicjdjfjdodjfjrj or foobar, but it’s just called arr because that’s a better name for a parameter that will be an array

nextInLine is the function itself, you can’t push to that, and item is the item you’re pushing into the array, you can’t push it into itself.

1 Like

You’re close. They’re not waiting to be declared, they’re waiting to be assigned a value, which is what I think you meant. But you’re correct that arr and item don’t have any meaning by themselves. arr isn’t an array just because of its name, but because of what gets passed when the function is called.

If you do this

// call the function with arguments
nextInLine([1,2,3], 4);

Then arr will be the array [1,2,3] and item will be the number 4. But if we switch the order

nextInLine(4, [1,2,3])

then arr will refer to the number 4 and item will be the array [1,2,3]. If you try this you’ll get an error and the code won’t work.

Another piece of the puzzle that you may be missing is that objects can have functions attached to them which are specific to the type of data it holds. When a function is attached to an object in this way, we tend to call them methods, but don’t get too hung up on that. For instance, the array [1,2,3] has the method push which takes an item and adds it to the end of the array that you called the method on. For example

[1,2,3].push(4) // [1,2,3,4]

Since this array wasn’t in a variable, it doesn’t get saved and so is tossed into the void, never to be seen again. Let’s save it.

var arr = [1,2,3];
// methods work when the data is stored in a variable
arr.push(4);
console.log(arr); // [1,2,3,4]

If you try to call an array specific method on something that isn’t an array, you’ll get an error and your program will crash.

var item = 4;
item.push(4); // TypeError: 4.push is not a function
1 Like