Created
April 24, 2017 18:37
-
-
Save rachel-yankelevitz/c049063f4f8c59fbce5fba099d795e04 to your computer and use it in GitHub Desktop.
Homework 3 underscore map.js description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = { ///<-- This is the creation of a variable called "_" which is a method | |
map: function(list, cbFn) { ///<-- on this line the first key value is function (so a function within the variable method). This function will take to params: list and cbFn | |
var newArray = []; /// within the method's function, there is a new local variable, newArray, which is an empty array | |
list.forEach(function(elem, index) { ///<-- on this line we're showing each element/item created by the parameter "list" | |
var newElem = cbFn(elem, index); ///<-- creating a new variable using the CbFn parameter and the elements and index generated by the list function | |
newArray.push(newElem); /// <-- the newArray variable is using the push vanilla JS method to create an array using the dynamic variables created by newElem | |
}); ///<-- this is closing the list.forEach function | |
return newArray; ///<-- this is the final result of the newArray varaible created by the nested function | |
} ///<-- this is closing the map keyword and function | |
}; ///<-- this is closing the var _ represents | |
_.map([1, 2, 3], function(num){ return num * 3; }); ///<-- this shows the result of the _ variable .map takes 3 objects within an array, converts them using a number parameter, multiplies them by three and generates a new array based on the new results. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/donoage/c59c0e9cd9f1c660fe6526cb2c9f8cf6
Hey Rachel,
See my link above. My comments start with
// Stephen:
. Check out how I would explain each line and your explanation.A few things as well,
//
comments above your code.//
Commenting in JS is 2 slashes not 3.<--
3/4. I think you're almost there!