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

206
Views
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
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!