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