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

189
Visualizações
how to check 3 possibilities using only 2 if statements

Create a function that receives an integer as an argument and returns a string such as:

  • if the number is multiple of 4, return "Foo"
  • if the number is multiple of 7, return "Bar"
  • if the number is multiple of 4 and 7, return "FooBar"

This can be done using 3 if statements like below but can this be done only using 2 if statements?

const intToStr = (intVal) => {

    if (intVal % 4 == 0 && intVal % 7 == 0) {
        return "FooBar";
    }
    
    if (intVal % 7 == 0) {
        return "Bar";
    } 
    
    if (intVal % 4 == 0) {
        return "Foo";
    }

}

console.log(intToStr(4*7));
console.log(intToStr(7));
console.log(intToStr(4));

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

0

Yes, it can be done with just 2 if statements.

const intToStr = (intVal) => {
  let str = '';

  if (intVal % 4 === 0) {
    str += "Foo";
  }

  if (intVal % 7 === 0) {
    str += "Bar";
  }
  
  return str;
}

console.log(intToStr(4*7));
console.log(intToStr(7));
console.log(intToStr(4));

about 4 years ago · Juan Pablo Isaza Relatório

0

Remember that you can sum strings. As value returned in case input is dividable by the same values as in two other ifs, you can just add strings in case input fulfills that condition.

To make it more clear:

var output = '';
if(intVal % 4 === 0)
{
  output = output + 'Foo' ;
}
if(intVal % 7 === 0)
{
  output = output + 'Bar' ;
}

return output;

That way you have two conditions and their cumulative values returned.

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