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.
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.
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?
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'
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.
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.