Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
How can I add 2 and 4 to odd numbers in JavaScript?

I am a beginner in Javascript now I have started, the only background I have is HTML and CSS. I'm trying to make a program that prints whether a number is even or odd. But to the odd numbers to add 2 and 4. My code :

function isEvenExceptTwoOrFour(number) { 
if (number%2 == 0  ) { 
 
 
    console.log("The number is even");}

 
else { 
    console.log("The number is odd ") 
} 
 
} 
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could write an if..else statement like this, using Logical Or (||) to check each of your conditions.

Below I used the statement

if (number === 2 || number === 4 || number % 2 === 1)

This checks if number === 2 or number === 4 or number % 2 === 1 (if the number is odd)

Code:

function isEvenExceptTwoOrFour(number) { 
  if (number === 2 || number === 4 || number % 2 === 1) {
    console.log("Number is considered odd");
  } else {
    console.log("Number is considered even")
  }
}

isEvenExceptTwoOrFour(1);
isEvenExceptTwoOrFour(2);
isEvenExceptTwoOrFour(6);

about 4 years ago · Juan Pablo Isaza Report

0

Write a function that accepts an array of exceptions, and returns a new function that accepts a number. The closure (the function that's returned) will then 1) check to see if the number is in the array, and return false otherwise 2) check to see if the number is even, and return true, otherwise 3) return false.

// Pass in the exceptions array and return a function
// that will accept a number
function checkIsEvenExcept(exceptions) {
  return function (n) {
    if (exceptions.includes(n)) return false;
    return n % 2 === 0 && true;
    return false;
  }
}

const exceptions = [2, 4, 18];

// Assign the result of calling `checkIsEvenExcept` with the
// exceptions array to a variable. This will be the function that
// we can call
const isEven = checkIsEvenExcept(exceptions);

// We can now call that function with a number
// that we need to check
console.log(isEven(6));
console.log(isEven(2));
console.log(isEven(1));
console.log(isEven(4));
console.log(isEven(8));
console.log(isEven(18));

about 4 years ago · Juan Pablo Isaza Report

0

You can just add conditions whether the number is 2 or 4.

 function isEvenExceptTwoOrFour(number) { 
   if ( number === 2 || number === 4 ||| number % 2 !== 0){
      console.log("The number is odd ") 
      return 
   }
   console.log("The number is even") 
} 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!