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

208
Vistas
if else is not working while making dice game in java script

I am doing a dice game challange on my Udemy course using javascript. Everything till now works fine, i made the dices to be random each time there's a reload. I'm now trying to make the "h1" to change to "DRAW!","PLAYER 1 WINS!" and "PLAYER 2 WINS!" using if else statements. But for some reason the if else wont respond . My Code: HTML:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Dicee</title>
    <link rel="stylesheet" href="C:\Users\natan\Desktop\Web Development udemy\JavaScript\Dicee Challenge\styles.css">
    <link href="https://fonts.googleapis.com/css?family=Indie+Flower|Lobster" rel="stylesheet">
  </head>
  <body>

    <div class="container">
      <h1 class ="h1">Refresh Me</h1>

      <div class="dice">
      <p>Player 1</p>
<img class="img1" src="C:\Users\natan\Desktop\Web Development udemy\JavaScript\Dicee Challenge\images\dice6.png" alt="dice6">
      </div>

      <div class="dice">
        <p>Player 2</p>
<img class="img2" src="C:\Users\natan\Desktop\Web Development udemy\JavaScript\Dicee Challenge\images\dice6.png" alt="dice6">
    </div>

    </div>



<script src="./js/index.js" charset="utf-8"></script>
  </body>

  <footer>
    www 🎲 App Brewery 🎲 com
  </footer>
</html>

JavaScript:

function randomDice() {

  const randomNum1 = Math.floor(Math.random() * 6 + 1);
  const randomNum2 = Math.floor(Math.random() * 6 + 1);

  const img1Random = document.querySelector(".img1");
  img1Random.setAttribute("src", "./images/dice" + randomNum1 + ".png");

  const img2Random = document.querySelector(".img2");
  img2Random.setAttribute("src", "./images/dice" + randomNum2 + ".png");

  let doch1 = document.querySelector("h1").innerHTML

  if (randomNum1 === randomNum2) {
    doch1 = "DRAW!"
  } else if (randomNum1 > randomNum2) {
    doch1 = "PLAYER 1 WINS!"
  } else {
    doch1 = "PLAYER 2 WINS!"
  }
}

randomDice()


about 4 years ago · Juan Pablo Isaza
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