This repository was archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (43 loc) · 1.49 KB
/
script.js
File metadata and controls
48 lines (43 loc) · 1.49 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
39
40
41
42
43
44
45
46
47
48
//Get inputs when submit button is clicked
document.getElementById('submit').onclick = function () {
//Get user input
var userChoice = document.getElementById('userChoice').value.toLowerCase();
//Take computer input
var computerChoice = Math.random();
//Convert computer input
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
console.log("User: " + userChoice);
//Compare the inputs
function compare (choice1, choice2) {
if (choice1 == choice2) {
return ("The result is a tie!");
} else if (choice1 == "rock") {
if (choice2 == "scissors") {
return ("rock wins");
} else {
return("paper wins");
};
} else if (choice1 == "paper") {
if (choice2 == "rock") {
return ("paper wins");
} else {
return ("scissors wins");
};
} else if (choice1 == "scissors") {
if (choice2 == "paper") {
return("scissors wins");
} else {
return("rock wins");
};
};
};
document.getElementById("computerChoice").innerHTML = "Computer: " + computerChoice;
document.getElementById("result").innerHTML = compare(userChoice, computerChoice);
};