Created
January 3, 2017 08:39
-
-
Save bepitulaz/511cc8819f2c004da504764f010ffa69 to your computer and use it in GitHub Desktop.
Higher-Order Functions Sample.
This file contains 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 sampleData = [1, 2, 3, 4, 5]; | |
/* | |
Buat fungsi yang mengalikan setiap elemen dengan angka 2. | |
Hasil yang diharapkan [2, 4, 6, 8, 10] | |
*/ | |
// Menggunakan perulangan for | |
function denganPerulangan(data) { | |
var expectedResult = []; | |
for(var i = 0; i < data.length; i++) { | |
var calculation = data[i] * 2; | |
expectedResult.push(calculation); | |
} | |
return expectedResult; | |
} | |
// Menggunakan map yang menerima fungsi lain sebagai parameternya | |
function denganMap(data) { | |
return data.map(function(item) { | |
return item * 2; | |
}); | |
} | |
denganPerulangan(sampleData); | |
denganMap(sampleData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment