-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (36 loc) · 972 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
*
* Title : Dice Challenge
* Author: Kean Duque
*
*/
"use strict";
class Dice {
constructor() {
const titleHolder = document.querySelector("h3");
const player1 = this.randomImage("img1");
const player2 = this.randomImage("img2");
titleHolder.innerHTML = this.checkWinner(player1, player2);
}
checkWinner(player1, player2) {
let playerWins = "";
playerWins =
player1 > player2 ? `🏆 Player 1 Wins!` : "Player 2 Wins! 🏆";
if (player1 === player2) playerWins = "Draw!";
return playerWins;
}
randomizeNumber() {
let randomNum = Math.random();
randomNum = Math.ceil(randomNum * 6);
return randomNum;
}
randomImage(className) {
let randomNumber = this.randomizeNumber();
let randomDiceImage = `dice${randomNumber}.png`;
let imgSrc = `images/${randomDiceImage}`;
let queryImg = document.querySelector(`.${className}`);
queryImg.setAttribute("src", imgSrc);
return randomNumber;
}
}
const dice = new Dice();