-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
65 lines (51 loc) · 1.63 KB
/
game.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
// Rock, Paper, Scissors game using Closures
var rps = function () {
return {
// generate the computer choice
computerChoice: function() {
var choice = Math.random();
if (choice < 0.34) {
return "rock";
} else if(choice <= 0.67) {
return "paper";
} else {
return "scissors";
}
}(),
play: function(userChoice) {
var output = "User chose " + userChoice + " and Computer chose " + this.computerChoice + ". \n";
if (userChoice === this.computerChoice) {
output += "Tie!";
return output;
}
if (userChoice === "rock") {
if (this.computerChoice === "scissors") {
output += "Rock Wins!";
}
else {
output += "Paper Wins!";
}
}
if (userChoice === "scissors") {
if (this.computerChoice === "rock") {
output += "Rock Wins!";
}
else {
output += "Scissors Wins!";
}
}
if (userChoice === "paper") {
if (this.computerChoice === "scissors") {
output += "Scissors Wins!";
}
else {
output += "Paper Wins!";
}
}
return output;
}
};
};
//var choice = prompt("Do you choose rock, paper or scissors?");
var game = rps();
console.log(game.play("rock"));