Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!