This file contains hidden or 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 _ = {}; | |
| _.pluck = function (list, key) { | |
| var result = []; | |
| for (var i = 0; i<list.length; i++) { | |
| if (list[i][key]) { | |
| result.push(list[i][key]); | |
| } | |
| } | |
| return result; |
This file contains hidden or 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
| function titleCase(str) { | |
| var strLowerCase = str.toLowerCase(); | |
| var split = strLowerCase.split(" "); | |
| for (var i = 0; i< split.length; i++) { | |
| var firstChar = split[i].charAt(0).toUpperCase(); | |
| split[i] = firstChar + split[i].substr(1); | |
| } | |
| var newStr = split.join(" "); | |
| return newStr; |
This file contains hidden or 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
| //your code here! | |
| /* | |
| Q1. Using your knowledge of JavaScript data types (i.e. numbers, strings, booleans, | |
| arrays and objects), represent our solar system as data (hint: the link is to a | |
| Wikipedia article – you should use it). You can include as many attributes as you | |
| like, but the following are required: | |
| - Age of the Solar System | |
| - Distance from the center of the galaxy | |
| - Number of known comets | |
| - The Planets |
This file contains hidden or 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
| // Call iterator(value, key, collection) for each element of collection. | |
| // Accepts both arrays and objects. | |
| // | |
| // Note: _.each does not have a return value, but rather simply runs the | |
| // iterator function over each item in the input collection. | |
| _.each = function(collection, iterator) { | |
| _.each(collection, iterator(value, key, collection) { | |
| } |
This file contains hidden or 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
| _.each = function(collection, iterator) { | |
| if (Array.isArray(collection)) { | |
| for (var i = 0; i<collection.length; i++) { | |
| return iterator(collection[i], i, collection); | |
| } | |
| } else { | |
| for (var i in collection) { | |
| return iterator(collection[i]); | |
| } |
This file contains hidden or 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 memoInCallback, itemInCallback; | |
| _.reduce(['item'], function(memo, item) { | |
| memoInCallback = memo; | |
| itemInCallback = item; | |
| }, 'memo'); | |
| expect(memoInCallback).to.equal('memo'); | |
| expect(itemInCallback).to.equal('item'); |
This file contains hidden or 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
| // Reduces an array or object to a single value by repetitively calling | |
| // iterator(accumulator, item) for each item. accumulator should be | |
| // the return value of the previous iterator call. | |
| // | |
| // You can pass in a starting value for the accumulator as the third argument | |
| // to reduce. If no starting value is passed, the first element is used as | |
| // the accumulator, and is never passed to the iterator. In other words, in | |
| // the case where a starting value is not passed, the iterator is not invoked | |
| // until the second element, with the first element as its second argument. |
This file contains hidden or 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
| _.reduce = function(collection, iterator, accumulator) { | |
| for (var i = 0; i < collection.length; i++) { | |
| accumulator = iterator(accumulator, collection[i]); | |
| } | |
| if (accumulator === undefined) { | |
| return collection[0]; | |
| } | |
| return accumulator; | |
| }; |
This file contains hidden or 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
| /* | |
| _.reduce = function(collection, iterator, accumulator) { | |
| for (var i = 0; i < collection.length; i++) { | |
| accumulator = iterator(accumulator, collection[i]); | |
| } | |
| if (accumulator === undefined) { | |
| return collection[0]; | |
| } | |
| return accumulator; | |
| }; |
This file contains hidden or 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 getElementsByClassName = function(className, n) { | |
| // your code here | |
| var resultArr = []; | |
| var searchClass = function(element) { | |
| if (element.classList) { | |
| if (element.classList.contains(className)) { | |
| resultArr.push(element); | |
| } | |
| } | |
| for (var i = 0; i < element.childNodes.length; i++) { |