Created
January 3, 2017 08:48
-
-
Save bepitulaz/35ca067ea4dd2dc8f4f0091aca753a51 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 menghasilkan Array yang isinya hanya bilangan genap. | |
Hasil yang diharapkan [2, 4] | |
*/ | |
// Menggunakan perulangan for | |
function denganPerulangan(data) { | |
var expectedResult = []; | |
for(var i = 0; i < data.length; i++) { | |
if(data[i] % 2 === 0) { | |
expectedResult.push(data[i]); | |
} | |
} | |
return expectedResult; | |
} | |
// Menggunakan filter yang menerima fungsi lain sebagai parameternya | |
function denganFilter(data) { | |
return data.filter(function(item) { | |
return item % 2 === 0; | |
}); | |
} | |
denganPerulangan(sampleData); | |
denganFilter(sampleData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment