Skip to content

Commit 2fae87b

Browse files
committed
Added a simple rock paper scissors game in javascript
1 parent 517e4cb commit 2fae87b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src = "RPS.js"></script>
5+
</head>
6+
<body>
7+
<h1>Make your selection</h1>
8+
<form name = "hand">
9+
<input type = "radio" name = "RPS" value = "rock">Rock<br>
10+
<input type = "radio" name = "RPS" value = "paper">Paper<br>
11+
<input type = "radio" name = "RPS" value = "scissors">Scissors<br>
12+
</form>
13+
<br>
14+
<input type = "button" value = "Submit" onclick="game()">
15+
<div id = "result"></div>
16+
</body>
17+
</html>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function game(){
2+
randNum = Math.floor(Math.random()*3)
3+
console.log(randNum)
4+
if(hand.RPS[0].checked == true){
5+
you = "rock"
6+
}
7+
else if(hand.RPS[1].checked == true){
8+
you = "paper"
9+
}
10+
else if(hand.RPS[2].checked == true){
11+
you = "scissors"
12+
}
13+
console.log(you)
14+
if(randNum == 0){
15+
randValue = "rock"
16+
}
17+
else if(randNum == 1){
18+
randValue = "paper"
19+
}
20+
else if(randNum == 2){
21+
randValue = "scissors"
22+
}
23+
console.log(randValue)
24+
if(you == randValue){
25+
returnResult= "You and the computer both chose " + you + ", therefore it is a tie";
26+
document.getElementById("result").innerHTML = returnResult
27+
}
28+
else if(you == "rock" && randValue == "paper"){
29+
returnResult = "You chose " + you + ", the computer chose " + randValue + ", so you lose";
30+
document.getElementById("result").innerHTML = returnResult
31+
}
32+
else if(you == "rock" && randValue == "scissors"){
33+
returnResult = "You chose " + you + ", the computer chose " + randValue + ", so you win";
34+
document.getElementById("result").innerHTML = returnResult
35+
}
36+
else if(you == "scissors" && randValue == "rock"){
37+
returnResult = "You chose " + you + ", the computer chose " + randValue + ", so you lose";
38+
document.getElementById("result").innerHTML = returnResult
39+
}
40+
else if(you == "scissors" && randValue == "paper"){
41+
returnResult = "You chose " + you + ", the computer chose " + randValue + ", so you win";
42+
document.getElementById("result").innerHTML = returnResult
43+
}
44+
else if(you == "paper" && randValue == "rock"){
45+
returnResult = "You chose " + you + ", the computer chose " + randValue + ", so you win";
46+
document.getElementById("result").innerHTML = returnResult
47+
}
48+
else if(you == "paper" && randValue == "scissors"){
49+
returnResult= "You chose " + you + ", the computer chose " + randValue + ", so you lose";
50+
document.getElementById("result").innerHTML = returnResult
51+
}
52+
}

0 commit comments

Comments
 (0)