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

170
Visualizações
How can I implement logics without if else, switch or a built-in function in javascript

Suppose we have function, which returns 3 when passing 5 to it and return 5 when passing 3 to it. I know it is very easy with if else and switch.

for example

function countReturn(number){
  if(number === 3){
  return 5
 }

  if(number === 5){
  return 3
 }

}

But how can I implement the same logic without if else, switch or any built-in function?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can store the return values in an array:

function countReturn(n) {
  let a = [,,,5,,3];
  return a[n];
}

That will return undefined for other inputs, just like your original code.

about 4 years ago · Juan Pablo Isaza Relatório

0

You could take the sum (3 + 5) and return the delta of the actual number.

function threeOrFive(value) {
    return 8 - value;
}

console.log(threeOrFive(3)); // 5
console.log(threeOrFive(5)); // 3

about 4 years ago · Juan Pablo Isaza Relatório

0

you can so it with ternary like this

return number === 3 ? 5 : (number === 5 ? 3 : 0);

Or you can do it with object

const obj = {
  "5": 3,
  "3": 5
};
return obj[number];
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