Last active
January 28, 2017 09:57
-
-
Save SK-CSE/99dd183b7b3f81356c091cab68217153 to your computer and use it in GitHub Desktop.
filter even and odd number in an array
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
function isEven(num) { | |
return num % 2 == 0; | |
} | |
function isOdd(num) { | |
return num % 2 != 0; | |
} | |
var nums = []; | |
for (var i = 0; i < 20; ++i) { | |
nums[i] = i+1; | |
} | |
var evens = nums.filter(isEven); // [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ] | |
console.log("Even numbers: "); | |
console.log(evens); | |
var odds = nums.filter(isOdd); // [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 ] | |
console.log("Odd numbers: "); | |
console.log(odds); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment