-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
38 lines (29 loc) · 1.1 KB
/
main.js
File metadata and controls
38 lines (29 loc) · 1.1 KB
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
function pickChoice(element) {
var userChoice = element.firstElementChild.src;
var humanBox = document.getElementsByClassName("human")[0].children[1];
humanBox.src = userChoice;
var uChoice = element.firstElementChild.alt;
var cChoice = CPUchoice();
showResults(cChoice, uChoice);
}
function CPUchoice() {
var options = ["rock", "paper", "scissors"];
var CPUbox = document.getElementsByClassName("cpu")[0].children[1];
var choice = options[randomNumberGenerator()];
CPUbox.src = "imgs/" + choice + ".svg";
return choice;
}
function randomNumberGenerator() {
var random = Math.round( Math.random() * 2);
return random;
}
function showResults(CPUchoice, userChoice) {
var resultsBox = document.getElementsByClassName("results")[0].firstElementChild;
if (CPUchoice == userChoice) {
resultsBox.innerHTML = "It's a draw.";
} else if (CPUchoice == "rock" && userChoice == "scissors" || CPUchoice == "paper" && userChoice == "rock" || CPUchoice == "scissors" && userChoice == "paper") {
resultsBox.innerHTML = "You lost.";
} else {
resultsBox.innerHTML = "You win!";
}
}