Could someone give a brief overview of why NaN is added to this return? Aren’t these doing the same thing?
Array.prototype.myMap = function(callback){
var newArray = [];
//option 1. works properly
for(var item "of" this) {
newArray.push(callback(item));
}
//option 2 adds NaN
for(var item "in" this) {
newArray.push(callback(this[item]);
}
return newArray;
};
var new_s = s.myMap(function(item){
return item * 2;
});
console.log(new_s); //using option 2
// returns [0,2,4,6,NaN]```
Thank you.
w3Schools Javascript loops:
* `for` - loops through a block of code a number of times
* `for/in` - loops through the properties of an object
* `for/of` - loops through the values of an iterable object
* `while` - loops through a block of code while a specified condition is true
* `do/while` - also loops through a block of code while a specified condition is true