How is the parameter (arr) linkied to the variable, var testArray

Tell us what’s happening:

Hi

This lesson threw me a bit. I’m finding it a bit difficult at the minute but Ill stick with it. in the code below we have used the arr. And there is a variable declared var testArray.

So I was setting arr = test array within the function but getting error messages.

So I watched the video.

I don’t get how it seem that it knows that arr and testArr are the same value but how?

There is nothing to link the 2. In fact I thought with out giving arr some value the code would not be able to carry out any process with out it having a definition.

Your code so far


function nextInLine(arr, item) {
 // Only change code below this line
 
 arr.push(item);
return arr.shift(0);



return item;
 // Only change code above this line
 

}

// Setup
var testArr = [1,2,3,4,5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63.

Challenge: Stand in Line

Link to the challenge:

You’re passing testArr in nextInLine function. It assigns testArr to arr. That’s how they are linked!

1 Like

NextInLine function accepts two arguments. First an array and an item.
whatever you pass as the first argument gets assigned to the “arr” variable and the second argument gets assigned to the “item” variable.

You are passing TestArr as the first argument to this function. So, your testArr is passed to the arr variable. Don’t get confused with the names.
Even if you call “arr” as “something” and testArr as “something_else”, as long as you pass “something_else” as the first argument to the said function, it will behave the same way.

Try changing their position and it won’t recognize that they are same. Because that’s what matters here. Try it and you will understand.

If it helps, you can think of the function call essentially saying

let removed = nextInLine(arr = testArr, item = 6);

Hi

Thanks for getting back. I just don’t get how arr as a variable use testArray as an argument just because its set out. If I use several array why is this one that taken as arr?

When you call the function, you are saying which array you want to use by passing the array in with your arguments.

Why testArr used as an argument? Because It’s there? If I had several array there how does arr as a parameter use testArr as an argument?

Look right here

// You call the function with testArr,
//  so the function uses testArr as arr!!!
// Also, this function call uses
//  6 as item
nextInLine(testArr, 6);

Thanks. Really struggling with this one. I just cant see how the function can porcess testArr within it when there is no referenec to it in the function.

function nextInLine(arr, item) {
 // ....
}

// Setup
let testArr = [1,2,3,4,5];

//        (arr, item)
nextInLine(testArr, 6);
// The function call itself says
//  'use testArr as the arr and 6 an the item'

Where does nextInLine(testArr, 6) come from?

That is the function call from above.

Arr is not a keyword is it?

No. arr is not a keyword. arr is the name of the array variable inside of the function.

I see that arr and testArr should have the same value but using arr instead of the arrays name and it still running and using testArr as if I has asked for testArr. To me logically I thought that it needs to be told that arr and testArr will have the same value.

That’s exactly what the function call does.

Jeremy thanks for your help here. Which line of code is calling the function?

That’s this line right here.

function myFunction(argument1, argument2, argument3) {
  // This is a function definition
}

myFunction(arg1, arg2, arg3); // This is a function call

Also if I change the name of the array from testArr to test its not recognising the array and reurning testArr undefined?

Well,

function nextInLine(arr, item) {
 // Only change code below this line
 arr.push(item);
return arr.shift(0);
return item;
 // Only change code above this line
}

// Setup
var testArr = [1,2,3,4,5]; // testArr is defined here

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // testArr is used here
console.log("After: " + JSON.stringify(testArr));

If you use a variable, you need to define it first. If you don’t define a variable, then it is undefined.