Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda