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

213
Views
How would I write this if else statement correctly?

I have written a program where there are three dice and 3 players. Player 1 rolls die 1, player 2 rolls die 2, player 3 rolls die 3. Whoever rolls the highest number (1-6) is the winner. I cannot figure out how to correctly write the if-else statement to correctly display who is the winner. Some code I have tried is:

    if (randomNumber1 > randomNumber2 || randomNumber3) {
    document.querySelector("h1").innerHTML = "Player 1 Wins! ๐Ÿšฉ";
    }
    else if (randomNumber2 > randomNumber1 || randomNumber3) {
    document.querySelector("h1").innerHTML = "Player 2 Wins! ๐Ÿšฉ";
    }
    else if (randomNumber3 > randomNumber1 || randomNumber2) {
    document.querySelector("h1").innerHTML = "Player 3 Wins! ๐Ÿšฉ";
    }
    else {
    document.querySelector("h1").innerHTML = "No Winner!";
    }


    if (randomNumber1 > randomNumber2 && randomNumber3) {
    document.querySelector("h1").innerHTML = "Player 1 Wins! ๐Ÿšฉ";
    }
    else if (randomNumber2 > randomNumber1 && randomNumber3) {
    document.querySelector("h1").innerHTML = "Player 2 Wins! ๐Ÿšฉ";
    }
    else if (randomNumber3 > randomNumber1 && randomNumber2) {
    document.querySelector("h1").innerHTML = "Player 3 Wins! ๐Ÿšฉ";
    }
    else {
    document.querySelector("h1").innerHTML = "No Winner!";
    }

So far these have not worked. Occasionally the program is correct as the highest roll is the winner, other times, for example, player 1 will have a 5 and player 3 will have a 6 and it tells me player 1 is the winner which is clearly not true. What am I missing? Please help. I am new to coding and I have been trying to figure this out all day. The best way for me to understand your answer is to please write out the correct code formatting for me. Please feel free to copy and edit the code I have already made.

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

Try this

if (randomNumber1 > randomNumber2 && randomNumber1 > randomNumber3) {
    document.querySelector("h1").innerHTML = "Player 1 Wins! ๐Ÿšฉ";
    }
    else if (randomNumber2 > randomNumber1 && randomNumber2 > randomNumber3) {
    document.querySelector("h1").innerHTML = "Player 2 Wins! ๐Ÿšฉ";
    }
    else if (randomNumber3 > randomNumber1 && randomNumber3 > randomNumber2) {
    document.querySelector("h1").innerHTML = "Player 3 Wins! ๐Ÿšฉ";
    }
    else {
    document.querySelector("h1").innerHTML = "No Winner!";
    }

So what you had wrong is the if statement. The && doesnt mean that the 1st parameter is gonna be compared with both of the others, it means that 2 comparisons need to be true for the if statement to go through.

Also dont forget to check if 2 or all players have the same result and thats the highest number.

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!