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

98
Visualizações
if else problem...why the console didnt print string 'error' but it printed 'Randommm' instead?

Nice to meet you all. I am new to programming here....my question is why the console didnt print string 'error' but it printed 'Randommm' instead? since it did not pass the first statement

const getUserChoice = (userInput) => {
      userInput = userInput.toLowerCase();
      if (userInput === "rock" || "scissors" || "paper" || "bomb") {
        return userInput;
      } else {
        console.log("Error");
      }
    };
    
    getUserChoice("Randommm"); //output:'nothing comes out instead of 'Error'

console.log(getUserChoice('rocK')) //output :rock (works fine)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your if statement isn't making a comparison in the 2nd,3rd and 4th conditions. You have to explicitly state what to compare against. You could do something like:

(userInput === "rock" || userInput === "scissors" ||userInput === "paper" || userInput === "bomb")

But i suggest you lift the words out to an array and do a comparison if the userinput exists within the array.

const choices = ["rock", "scissors", "paper", "bomb"];

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (choices.includes(userInput)) {
    return userInput;
  } else {
    console.log("Error");
  }
};

console.log(getUserChoice("Randommm")); //output:'Error' instead of 'Randommm'

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