-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.js
66 lines (60 loc) · 1.5 KB
/
rps.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
var choices = ["rock", "paper", "scissors"];
var i = Math.floor(Math.random() * 3);
var CompChoice = choices[i];
var UserPoints = 0;
var ComPoints = 0;
function main(UserChoice)
{
var user = document.getElementById("userResult");
user.innerHTML = display(UserChoice);
var comp = document.getElementById("compResult");
comp.innerHTML = display(CompChoice);
if(UserChoice === "paper" && CompChoice === "rock" || UserChoice === "rock" && CompChoice === "scissors" ||
UserChoice === "scissors" && CompChoice === "paper")
{
win();
}
else if(UserChoice === CompChoice)
{
draw();
}
else
{
lose();
}
function computerGame()
{
i = Math.floor(Math.random() * 3);
CompChoice = choices[i];
}
setTimeout(computerGame, 1200);
userScore.innerText = UserPoints;
compScore.innerText = ComPoints;
}
function display(choose)
{
if(choose === "rock")
return '<img src="img/hand-rock-regular.svg" alt="">';
else if(choose === "paper")
return '<img src="img/hand-paper-regular.svg" alt="">';
else
return '<img src="img/hand-scissors-regular.svg" alt="">';
}
function win()
{
UserPoints++;
document.getElementById("result_text").innerHTML = "You win!";
}
function draw()
{
document.getElementById("result_text").innerHTML = "It's a Draw.";
}
function lose()
{
ComPoints++;
document.getElementById("result_text").innerHTML = "You lose!";
}
function myFunction() {
window.location.reload();
alert("Do you want to Start New Game");
}