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

214
Visualizações
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 Respostas
Responde à pergunta

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 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