Skip to content

Instantly share code, notes, and snippets.

View haingdc's full-sized avatar

Hai.Nguyen Github haingdc

View GitHub Profile
@haingdc
haingdc / colorArray.js
Created February 28, 2018 10:30
A Gentle Introduction to Functional JavaScript: Part 2
var colours = [
'red', 'orange', 'yellow',
'green', 'blue', 'purple'
];
for (var i = 0; i < colours.length; i = i + 1) {
addColour(colours[i]);
}
@haingdc
haingdc / forEach.js
Created February 28, 2018 10:32
A Gentle Introduction to Functional JavaScript: Part 2
function forEach(callback, array) {
for (var i = 0; i < array.length; i = i + 1) {
callback(array[i], i);
}
}
@haingdc
haingdc / call_forEach.js
Created February 28, 2018 10:33
A Gentle Introduction to Functional JavaScript: Part 2
forEach(addColour, colours);
@haingdc
haingdc / buildIn_forEach.js
Created February 28, 2018 10:35
A Gentle Introduction to Functional JavaScript: Part 2
var colours = [
'red', 'orange', 'yellow',
'green', 'blue', 'purple'
];
colours.forEach(addColour);
@haingdc
haingdc / getDomsFromIds.js
Created February 28, 2018 10:39
A Gentle Introduction to Functional JavaScript: Part 2
var ids = ['unicorn', 'fairy', 'kitten'];
// elements sẽ chứa các phần tử DOM
var elements = [];
for (var i = 0; i < ids.length; i = i + 1) {
elements[i] = document.getElementById(ids[i]);
}
@haingdc
haingdc / map.js
Created February 28, 2018 10:40
A Gentle Introduction to Functional JavaScript: Part 2
var map = function(callback, array) {
var newArray = [];
for (var i = 0; i < array.length; i = i + 1) {
newArray[i] = callback(array[i], i);
}
return newArray;
}
@haingdc
haingdc / call_map.js
Created February 28, 2018 10:41
A Gentle Introduction to Functional JavaScript: Part 2
var getElement = function(id) {
return document.getElementById(id);
};
var elements = map(getElement, ids);
@haingdc
haingdc / buildIn_Map.js
Created February 28, 2018 10:43
A Gentle Introduction to Functional JavaScript: Part 2
var ids = ['unicorn', 'fairy', 'kitten'];
var getElement = function(id) {
return document.getElementById(id);
};
var elements = ids.map(getElement);
@haingdc
haingdc / twoExamples.js
Created February 28, 2018 10:46
A Gentle Introduction to Functional JavaScript: Part 2
// ví dụ mảng số
var numbers = [1, 3, 5, 7, 9];
var total = 0;
for (i = 0; i < numbers.length; i = i + 1) {
total = total + numbers[i];
}
// tổng là 25
// mảng từ
var words = ['sparkle', 'fairies', 'are', 'amazing'];
@haingdc
haingdc / twoExamples2.js
Created February 28, 2018 10:47
A Gentle Introduction to Functional JavaScript: Part 2
var add = function(a, b) {
return a + b;
}
var numbers = [1, 3, 5, 7, 9];
var total = 0;
for (i = 0; i < numbers.length; i = i + 1) {
total = add(total, numbers[i]);
}
// tổng là 25