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 reduce = function(callback, initialValue, array) { | |
var output = initialValue; | |
for (var i = 0; i < array.length; i = i + 1) { | |
output = callback(output, array[i]); | |
} | |
return output; | |
}; |
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 total = reduce(add, 0, numbers); | |
var sentence = reduce(joinWord, '', words); |
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 total = numbers.reduce(add, 0); | |
var sentence = words.reduce(joinWord, ''); |
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 ponies = [ | |
[ | |
['name', 'Fluttershy'], | |
['image', 'http://tinyurl.com/gpbnlf6'], | |
['description', 'Fluttershy is a female Pegasus pony and one of the main characters of My Little Pony Friendship is Magic.'] | |
], | |
[ | |
['name', 'Applejack'], | |
['image', 'http://tinyurl.com/gkur8a6'], | |
['description', 'Applejack is a female Earth pony and one of the main characters of My Little Pony Friendship is Magic.'] |
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 addToObject = function(obj, arr) { | |
obj[arr[0]] = arr[1]; // thêm thuộc tính cho obj | |
return obj; | |
}; |
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 ponyArrayToObject = function(ponyArray) { | |
return reduce(addToObject, {}, ponyArray); | |
}; |
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 tidyPonies = map(ponyArrayToObject, ponies); |
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 t(s,o){ | |
for(var p in o) | |
s=s.replace(new RegExp('{'+p+'}','g'), o[p]); | |
return s; | |
} | |
var data = { name: "July", age: "seventeen" }; | |
t("When {name} was just {age}!", data); | |
// "When July was just seventeen!" |
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 ponyToListItem = function(pony) { | |
var template = '<li><img src="{image}" alt="{name}"/>' + | |
'<div><h3>{name}</h3><p>{description}</p>' + | |
'</div></li>'; | |
return t(template, pony); | |
} |
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 ponyList = map(ponyToListItem, tidyPonies); | |
var html = '<ul>' + reduce(joinWord, '', ponyList) + '</ul>'; |