So I finished this challenge, and it was pretty straight forward, but one thing my brain isn’t quite understanding, is where they got “val” from in the first place; I get that val is an argument for the map’s function and I get that it’s supposed to be the specific index for the array at a given iteration, what I don’t understand is how the function KNOWS that val is supposed to be that index. like I could understand having something like a for loop where you declare a variable ‘val’
but why / how does the map know to put the index value in that postition?
Sorry for the weird question, If it helps I did read up on maps, but it didn’t really answer my question. I feel like it’s just something simple right infront of me that I’m missing.
Your code so far
var oldArray = [1,2,3,4,5];
// Only change code below this line.
var newArray = oldArray.map(function(val){ // how is 'val' getting the index & its value?
return val + 3;
});
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/iterate-over-arrays-with-map