Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda