-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
38 lines (27 loc) · 1.16 KB
/
script.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
29
30
31
32
33
34
35
36
37
38
var randomNumber1 = Math.random();
randomNumber1 = randomNumber1 * 6 + 1;
randomNumber1 = Math.floor(randomNumber1);
var randomDiceImage = "dice" + randomNumber1 + ".png"; //to get image dice1 to dice6
var randomImageSource = "images/" + randomDiceImage; // images/dice1 to images/dice6
var image1 = document.querySelectorAll("img")[0]; // select first img tag
image1.setAttribute("src", randomImageSource);
//for dice 2
var randomNumber2 = Math.random();
randomNumber2 = randomNumber2 * 6 + 1;
randomNumber2 = Math.floor(randomNumber2);
var randomDiceImage2 = "dice" + randomNumber2 + ".png";
var randomImageSource2 = "images/" + randomDiceImage2;
var image2 = document.querySelectorAll("img")[1];
image2.setAttribute("src",randomImageSource2);
//Selecting Winner
if(randomNumber1 > randomNumber2){
document.querySelector("h1").innerHTML = "🚩Play 1 Wins";
}
else{
if(randomNumber1 < randomNumber2){
document.querySelector("h1").innerHTML = "Player 2 wins🚩";
}
else{
document.querySelector("h1").innerHTML = "Draw!";
}
}