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 colours = [ | |
'red', 'orange', 'yellow', | |
'green', 'blue', 'purple' | |
]; | |
for (var i = 0; i < colours.length; i = i + 1) { | |
addColour(colours[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
function forEach(callback, array) { | |
for (var i = 0; i < array.length; i = i + 1) { | |
callback(array[i], 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
forEach(addColour, colours); |
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 colours = [ | |
'red', 'orange', 'yellow', | |
'green', 'blue', 'purple' | |
]; | |
colours.forEach(addColour); |
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 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]); | |
} |
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 map = function(callback, array) { | |
var newArray = []; | |
for (var i = 0; i < array.length; i = i + 1) { | |
newArray[i] = callback(array[i], i); | |
} | |
return newArray; | |
} |
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 getElement = function(id) { | |
return document.getElementById(id); | |
}; | |
var elements = map(getElement, ids); |
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 ids = ['unicorn', 'fairy', 'kitten']; | |
var getElement = function(id) { | |
return document.getElementById(id); | |
}; | |
var elements = ids.map(getElement); |
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
// 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']; |
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 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 |