Skip to content

Commit

Permalink
Merge pull request #132 from gabrielkunkel/grk-assign
Browse files Browse the repository at this point in the history
exercise 19 switched Object.create() over to Object.assign()
  • Loading branch information
morenoh149 committed Feb 5, 2016
2 parents 4193795 + 21648db commit f8d0a2c
Showing 1 changed file with 17 additions and 43 deletions.
60 changes: 17 additions & 43 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2146,29 +2146,15 @@ <h4>Exercise 19: Reducing with an initial value</h4>
// ]
return videos.
reduce(function(accumulatedMap, video) {
var obj = {};

// Object.create() makes a fast copy of the accumulatedMap by
// creating a new object and setting the accumulatedMap to be the
// new object's prototype.
// Initially the new object is empty and has no members of its own,
// except a pointer to the object on which it was based. If an
// attempt to find a member on the new object fails, the new object
// silently attempts to find the member on its prototype. This
// process continues recursively, with each object checking its
// prototype until the member is found or we reach the first object
// we created.
// If we set a member value on the new object, it is stored
// directly on that object, leaving the prototype unchanged.
// Object.create() is perfect for functional programming because it
// makes creating a new object with a different member value almost
// as cheap as changing the member on the original object!

var copyOfAccumulatedMap = Object.create(accumulatedMap);

// ----- INSERT CODE TO ADD THE VIDEO TITLE TO THE ----
// ----- NEW MAP USING THE VIDEO ID AS THE KEY ----

return copyOfAccumulatedMap;
// ----- INSERT CODE TO ADD THE VIDEO TITLE TO THE ----
// ----- NEW MAP USING THE VIDEO ID AS THE KEY ----

// Object.assign() takes all of the enumerable properties from
// the object listed in its second argument (obj) and assigns them
// to the object listed in its first argument (accumulatedMap).
return Object.assign(accumulatedMap, obj);
},
// Use an empty map as the initial value instead of the first item in
// the list.
Expand Down Expand Up @@ -2230,28 +2216,16 @@ <h4>Exercise 19: Reducing with an initial value</h4>
// ]
return videos.
reduce(function(accumulatedMap, video) {
var obj = {};

// ----- INSERT CODE TO ADD THE VIDEO TITLE TO THE ----
// ----- NEW MAP USING THE VIDEO ID AS THE KEY ----
obj[video.id] = video.title;

// Object.create() makes a fast copy of the accumulatedMap by
// creating a new object and setting the accumulatedMap to be the
// new object's prototype.
// Initially the new object is empty and has no members of its own,
// except a pointer to the object on which it was based. If an
// attempt to find a member on the new object fails, the new object
// silently attempts to find the member on its prototype. This
// process continues recursively, with each object checking its
// prototype until the member is found or we reach the first object
// we created.
// If we set a member value on the new object, it is stored
// directly on that object, leaving the prototype unchanged.
// Object.create() is perfect for functional programming because it
// makes creating a new object with a different member value almost
// as cheap as changing the member on the original object!

var copyOfAccumulatedMap = Object.create(accumulatedMap);

copyOfAccumulatedMap[video.id] = video.title;

return copyOfAccumulatedMap;
// Object.assign() takes all of the enumerable properties from
// the object listed in its second argument (obj) and assigns them
// to the object listed in its first argument (accumulatedMap).
return Object.assign(accumulatedMap, obj);
},
// Use an empty map as the initial value instead of the first item in
// the list.
Expand Down

0 comments on commit f8d0a2c

Please sign in to comment.