-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
81 lines (66 loc) · 2.14 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
let winMsg = "Victory";
let loseMsg = "Defeat";
let tieMsg = "Tie";
let moveList = ["Rock", "Paper", "Scissors"];
let statusDisplay = document.querySelector("#status-head");
let moveDisplays = document.querySelectorAll(".move-display h2");
let buttons = document.querySelectorAll("button");
/**
* Computes result of the game. returns victory message if move 1 wins.
* @param {Number} move1 move 1
* @param {Number} move2 move 2
* @return {String} result result of the game
*/
function calcResult(move1, move2) {
var result = move1 - move2;
if (result == 1 || result + 3 == 1) {
return winMsg;
} else if (result == 2 || result + 3 == 2) {
return loseMsg;
} else {
return tieMsg;
}
}
/**
* @return {Number} random number between 0 and 2
*/
function randomMove() {
return Math.floor(Math.random() * 3);
}
/**
* Displays start state of game
*/
function startGame() {
statusDisplay.textContent = "Choose!";
buttons.forEach((button, index) => {
button.textContent = moveList[index];
button.style.display = "inline-block";
buttons[index].removeEventListener("click", startGame);
buttons[index].addEventListener("click", endGame);
});
moveDisplays.forEach((moveDisplay) => (moveDisplay.style.display = "none"));
}
/**
* Displays end state of game
* @param {Event} event event containing information of users input.
*/
function endGame(event) {
let playerMove = moveList.indexOf(event.target.textContent);
let computerMove = randomMove();
statusDisplay.textContent = calcResult(playerMove, computerMove);
moveDisplays.forEach(
(moveDisplay) => (moveDisplay.style.display = "inline-block")
);
moveDisplays[0].textContent = `You played ${moveList[playerMove]}`;
moveDisplays[1].textContent = `Computer played ${moveList[computerMove]}`;
buttons.forEach((button, index) => {
if (index == 1) {
button.textContent = "Play Again";
button.removeEventListener("click", endGame);
button.addEventListener("click", startGame);
} else {
button.style.display = "none";
}
});
}
startGame();