forked from rocketacademy/fundamentals-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (114 loc) · 3.31 KB
/
script.js
File metadata and controls
123 lines (114 loc) · 3.31 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//prompt for name
var user = prompt("Please enter your name");
if (user == null) {
var user = prompt("No name recorded. Please enter your name");
}
if (user != null) {
alert(
"Hi " +
user +
"!" +
" Please input Rock, Paper or Scissors in the Input Box to start playing"
);
}
//declare global variable
var winCounter = 0;
var loseCounter = 0;
var totalTries = 0;
var main = function (input) {
var myOutputValue = "Please input Rock, Paper or Scissors";
var computerShows = computerGuess();
//if user chooses rock, computer chooses scissors, user wins
//if user chooses scissors, computer chooses paper, user wins
//if user chooses paper, computer chooses rock, user wins
if (
(input == "rock" && computerShows == "scissors") ||
(input == "paper" && computerShows == "rock") ||
(input == "scissors" && computerShows == "paper")
) {
winCounter = winCounter + 1;
var winPercentage = (
100 *
(winCounter / (winCounter + loseCounter))
).toFixed(2);
totalTries = totalTries + 1;
console.log("winCount");
console.log(winCounter);
var myOutputValue =
user +
" wins! <br><br> You guessed " +
input +
" while Computer guessed " +
computerShows +
"<br> Out of " +
totalTries +
" tries, you have won " +
winCounter +
" times";
}
//if user chooses rock, computer chooses paper, user loses
//if user chooses scissors, computer chooses rock, user loses
//if user chooses paper, computer chooses scissors, user loses
if (
(input == "rock" && computerShows == "paper") ||
(input == "paper" && computerShows == "scissors") ||
(input == "scissors" && computerShows == "rock")
) {
loseCounter = loseCounter + 1;
console.log("loseCount");
console.log(loseCounter);
var winPercentage = (
100 *
(winCounter / (winCounter + loseCounter))
).toFixed(2);
totalTries = totalTries + 1;
var myOutputValue =
user +
" loses! <br><br> You guessed " +
input +
" while Computer guessed " +
computerShows +
"<br> Out of " +
totalTries +
" tries, you have won " +
winCounter +
" times";
}
//if user chooses rock, computer chooses rock, they draw
//if user chooses scissors, computer chooses scissors, they draw
//if user chooses paper, computer chooses paper, they draw
if (
(input == "rock" && computerShows == "rock") ||
(input == "paper" && computerShows == "paper") ||
(input == "scissors" && computerShows == "scissors")
) {
totalTries = totalTries + 1;
var myOutputValue =
"It's a draw! <br><br> You guessed " +
input +
" while Computer guessed " +
computerShows +
"<br> Out of " +
totalTries +
" tries, you have won " +
winCounter +
" times";
}
return myOutputValue;
};
//computer will generate either rock, scissors or paper
var computerGuess = function () {
var randomDecimal = Math.random();
var randomNumber = Math.floor(randomDecimal * 3);
var randomNumberPlusOne = randomNumber + 1;
if (randomNumberPlusOne == 1) {
var computerOutput = "rock";
}
if (randomNumberPlusOne == 2) {
var computerOutput = "paper";
}
if (randomNumberPlusOne == 3) {
var computerOutput = "scissors";
}
return computerOutput;
};