Member-only story
Mastering Even & Odd Number Logic in JavaScript
Here are some examples to demonstrate solving “Even-Odd” problems in JavaScript Using the filter() method and forEach() Method:
Problem 1: Check whether a number is even or odd
function checkEvenOdd(num){
if (num % 2 === 0) {
console.log(num + " This is even");
}else{
console.log(num + " this is odd");
}
}
checkEvenOdd(7); //output : 7 is odd;Problem 2: Print all even numbers from an array
let Num = [1, 2, 3, 4, 5, 6, 7]
function printEvenNum(array) {
array.forEach(num =>{
if(num % 2 === 0){
console.log(num + " " + "even Numbers");
}
})
}
printEvenNum(Num);
//Output:
//2 Even Numbers
//4 Even Numbers
//6 Even NumbersProblem 3: Print all odd numbers from an array
let Num = [1, 2, 3, 4, 5, 6, 7]
function printOddNum(array) {
array.forEach(num =>{
if(num % 2 !== 0){
console.log(num + " " + "odd Number")
}
});
}
printOddNum(Num);
//Output :
//1 odd Number
//3 odd Number
//5 odd Number
//7 odd NumberProblem 4: Count the even and odd numbers from an array
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function…







