-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
28 lines (22 loc) · 819 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// dicee randomize
function generateDice() {
var randomDice = Math.floor(Math.random() * 6) + 1; // Random from 1 to 6
return randomDice;
}
function generateName(dice){
var randomDiceImage = "dice" + dice + ".png"; // Sets the dice name
var randomImgSrc = "images/" + randomDiceImage; // Sets the image path
return randomImgSrc;
}
var dice1 = generateDice();
var dice2 = generateDice();
document.querySelector(".img1").setAttribute("src", generateName(dice1));
document.querySelector(".img2").setAttribute("src", generateName(dice2));
// Telling who won the game
if (dice1 > dice2) {
document.querySelector("h1").innerHTML = "Player 1 wins";
} else if (dice1 < dice2) {
document.querySelector("h1").innerHTML = "Player 2 wins";
} else {
document.querySelector("h1").innerHTML = "Draw!";
}