Skip to content

Instantly share code, notes, and snippets.

@SK-CSE
Last active January 28, 2017 09:57
Show Gist options
  • Save SK-CSE/99dd183b7b3f81356c091cab68217153 to your computer and use it in GitHub Desktop.
Save SK-CSE/99dd183b7b3f81356c091cab68217153 to your computer and use it in GitHub Desktop.
filter even and odd number in an array
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