Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rachel-yankelevitz/c049063f4f8c59fbce5fba099d795e04 to your computer and use it in GitHub Desktop.
Save rachel-yankelevitz/c049063f4f8c59fbce5fba099d795e04 to your computer and use it in GitHub Desktop.
Homework 3 underscore map.js description
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.
@donoage
Copy link

donoage commented Apr 29, 2017

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,

  • Put your // comments above your code.
  • // Commenting in JS is 2 slashes not 3.
  • No need for <--

3/4. I think you're almost there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment