-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
87 lines (80 loc) · 2.47 KB
/
script.js
File metadata and controls
87 lines (80 loc) · 2.47 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
//global scope state variables so they dont get reset in every run
var resultThisRound = "";
var computerWins = 0;
var userWins = 0;
var draws = 0;
var totalRounds = 0;
var userName = "anonymous";
var main = function (input) {
var finalOutput = "";
//convert input into lower case
input = input.toLowerCase();
if (validateInput(input) == "invalid") {
finalOutput = "Oops! You can only choose 'Stone', 'Paper' or 'Scissors'";
return finalOutput;
} else {
let computerWord = generateComputerChoice();
if (input == computerWord) {
resultThisRound = "It's a draw";
draws++;
} else if (input == "paper" && computerWord == "stone") {
resultThisRound = "You win";
userWins++;
} else if (input == "paper" && computerWord == "scissors") {
resultThisRound = "You lose";
computerWins++;
} else if (input == "stone" && computerWord == "paper") {
resultThisRound = "You lose";
computerWins++;
} else if (input == "stone" && computerWord == "scissors") {
resultThisRound = "You lose";
computerWins++;
} else if (input == "scissors" && computerWord == "paper") {
resultThisRound = "You win";
userWins++;
} else if (input == "scissors" && computerWord == "stone") {
resultThisRound = "You lose";
computerWins++;
}
totalRounds++;
finalOutput =
resultThisRound +
" this round. You have played " +
totalRounds +
" rounds so far. You have won " +
userWins +
" out of " +
totalRounds +
" which is " +
Math.floor((userWins / totalRounds) * 100) +
"% and the computer has won " +
computerWins +
" out of " +
totalRounds +
" which is " +
Math.floor((computerWins / totalRounds) * 100) +
"% ";
}
return finalOutput;
};
//Validate input, called in Main after input is cast to lower case. !important that return value is exact string as typed here
var validateInput = function (input) {
if (input == "paper" || input == "stone" || input == "scissors") {
return "valid";
} else {
return "invalid";
}
};
//Refactored to put this in separate function!
var generateComputerChoice = function () {
var computerChoice = Math.floor(Math.random() * 3);
var computerWord = "";
if (computerChoice == 0) {
computerWord = "stone";
} else if (computerChoice == 1) {
computerWord = "paper";
} else if (computerChoice == 2) {
computerWord = "scissors";
}
return computerWord;
};