Trying to understand how i could have known this

Sorry for phone pic, printscrn isn’t working. Anyways i finally figured this out with some help. The tests (bottom left) definitely made it seem more complicated than it was. Anyways, looking through this code is there any way i could have known “arr” was linked to “testArr”? I posted a picture of the solved answer which needed to use arr.push(). I was doing it right from the beginning but i kept using testArr.push() because that is what i see in the code as the array.
5 Replies
vince
vince16mo ago
The function argument is labelled as arr so you use arr inside the function
DJR
DJR16mo ago
is there any way i could have known “arr” was linked to “testArr”?
The function accepts (and expects) two arguments, arr and item. When you call the function, you include both of these arguments. As the first argument given is the array testArr, that will be used to substitute for arr. 6 is substituted for item.
keyohh
keyohh16mo ago
Yeah i see that now in the console log. Still don’t 100% get why i had to use arr.push instead of testArr. Feel like they should have let you know somewhere. In my mind was just looking at arr as the array (testArr) parameter. So i was thinking testArr was supposed to be plugged in there.
DJR
DJR16mo ago
arr.push() works because you're associating the first parameter arr with an array when you run the function with testArr used as the substitute for the first parameter.
Chris Bolson
Chris Bolson16mo ago
Ad the end of the day "testArr" and "arr" are just made-up names referring to the data (which is an array in this case). - "testArr" is the name you are giving to the original array of data - "arr" is the name that you are giving to any array passed to the nextInLine function. The basic idea of a function is that it can be used multiple times and with values. (in this case arrays) passed to it from any source, not just the "testArr" that is being defined in your example. Imagine that you had 2 arrays of data:
let testArr = [1,2,3,4,5];
let testArr2 = [6,7,8,9];
let testArr = [1,2,3,4,5];
let testArr2 = [6,7,8,9];
You then want to call the function for each of these arrays (separately). It would be even more confusing if the function was defined as function nextInLine(testArr, item){.... as this might lead you to think that it only accepts the first of your arrays whereas this is not the case, it simply expects an array. This is why it makes more sense to use a non-specific name for the array parameter in the function (in this case it is "arr" ). Whatever array is being passed to it, will be called "arr" within the scope of the function - you are not renaming it globally., just within the function.
Want results from more Discord servers?
Add your server