Hi,
Short answer - yes. num
is just variable local to the callback so name it anything.
Let me explain a different way with an example building up to map. I think once you discover that map is just a fancy for loop that does something to each element youāll see why it does not matter what you name the function arguments in your callback.
.map() does āsomethingā to each element of an array and saves the results of that something in a new array. That something is your whatever you callback does.
For instance, you have an array of numbers and you want a new array of those numbers tripled. Without array methods you could do this with a for loop.
const arr = [1,2,3,4];
let newArray = [];
for(let i = 0; i < arr.length; i++){
newArray[i] = arr[i] * 3;
}
console.log(newArray); //[ 3, 6, 9, 12 ]
Maybe you already have a function that triples a number. You could use that in your for loop too.
const arr = [1,2,3,4];
newArray = [];
// your triple function
function triple(someNum){
return someNum * 3;
}
for(let i = 0; i < arr.length; i++){
newArray[i] = triple(arr[i])
}
console.log(newArray); //[ 3, 6, 9, 12 ]
You can do the same with map. It takes care of the for loop for you. Really you just provide the āsomethingā that gets performed on each element - a callback that triples a number.
const arr = [1,2,3,4];
newArray = arr.map(function(someNumber){
return someNumber * 3;
});
console.log(newArray); //[ 3, 6, 9, 12 ]
Since it looks like this challenge is using arrow functions we can do that too.
const arr = [1,2,3,4];
const newArray = arr.map( aNum => aNum * 3);
console.log(newArray); //[ 3, 6, 9, 12 ]
In fact, you could even use your triple function from the earlier example with map.
const arr = [1,2,3,4];
const newArray = arr.map(triple);
console.log(newArray); //[ 3, 6, 9, 12 ]
OK - about those names in callback argument list. Revisiting the second example notice that it makes no difference what we call that number in the triple function. Also notice that sending extra parameters (like map does) changes nothing - the extras are simply ignored.
const arr = [1,2,3,4];
newArray = [];
// my triple function
function triple(x){ // NEW local variable name x makes no difference
return x * 3;
}
for(let i = 0; i < arr.length; i++){
newArray[i] = triple(arr[i], i, arr); // sending more parameters makes no difference
}
console.log(newArray); //[ 3, 6, 9, 12 ]
Same when using map. Changing argument name makes no difference.
const arr = [1,2,3,4];
const newArray = arr.map( theNum => theNum * 3);
console.log(newArray); //[ 3, 6, 9, 12 ]