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
| // currying example | |
| var add = function(vala){ | |
| return function(valb){ | |
| return vala + valb; | |
| } | |
| } | |
| console.log(add(2)(2)); // outputs 4 | |
| // or more currying just not chained which is the same thing |
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 allStorage() { | |
| var archive = [], | |
| keys = Object.keys(localStorage), | |
| i = 0, key; | |
| for (; key = keys[i]; i++) { | |
| archive.push( key + '=' + localStorage.getItem(key)); | |
| } |
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 cars = [ {make: "Nissan", | |
| model: "Maxima"}, | |
| {make: "Infinity", | |
| model: "G60RS"} | |
| ]; | |
| function pluck(array, property) { | |
| var results = []; | |
| array.map(function(item){ | |
| results.push(item[property]); | |
| }); |
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 cars = [{make: "Honda", type: "truck"},{make: "Cadillac", type: "sedan"}, {make: "McLaren", type: "coup"}, {make: "Infinity", type: "coup"}]; | |
| function reject(array, iteratorFunction) { | |
| return array.filter(iteratorFunction); | |
| } | |
| var noCoups = reject(cars, function(car) { | |
| return car.type !== "coup"; | |
| }); | |
| // or numbers reusing 'filter' to reverse the effect of filter (only push items that return false) | |
| var numbers = [10,20,30]; |
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 ladders= [{height: '20 feet'}, {height: '10 feet'},{height: '100 feet'}]; | |
| function findWhere(array, criteria) { | |
| return array.find(function(item){ | |
| return item.height === criteria.height; | |
| }); | |
| } | |
| var twentyFootLadder = findWhere(ladders, {height: '20 feet'}); |
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 miles = [{ distance: 34 }, { distance: 12 } , { distance: 1 }]; | |
| var totalDistance = miles.reduce(function(prev, item){return prev + item.distance;}, 0); | |
| //return the total of sitting and standing chairs | |
| var desks = [ | |
| { type: 'sitting' }, | |
| { type: 'standing' }, | |
| { type: 'sitting' }, | |
| { type: 'sitting' }, | |
| { type: 'standing' } |
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 makeUnique(array) { | |
| return array.reduce(function(prev, num) { | |
| if (!prev.find(function(item) { return item == num; })) { | |
| prev.push(num); | |
| } | |
| return prev; | |
| }, []); | |
| } | |
| var uniqueNumbers = makeUnique([1, 6 ,5, 6, 4, 3, 3, 3, 2, 1, 1]); |
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
| const points = [ | |
| [1, 2], | |
| [50, -10], | |
| [110, 405] | |
| ]; | |
| points.map(([x, y]) => { | |
| return { x, y }; | |
| }); |
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 double([number, ...rest]) { | |
| return !number ? [] : [number * 2, ...double(rest)]; | |
| } |
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 compareTriplets(a, b) { | |
| let score1, score2, iter; | |
| score1 = score2 = iter = 0; | |
| a.map(item => { | |
| if (item > b[iter]) { | |
| score1++; | |
| } | |
| if ( item < b[iter]) { | |
| score2++; | |
| } |
OlderNewer