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
126 lines (125 loc) · 3.58 KB
/
script.js
File metadata and controls
126 lines (125 loc) · 3.58 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
124
125
126
// Winrate
var correctGuesses = 0;
var numberOfTries = 0;
var correctGuessesRate = correctGuesses + "/" + numberOfTries;
// Game
var main = function (input) {
var computer = computerWord();
// Default output
var myOutputValue = "Please input 'banana', 'chisel' or 'faucet'.";
// If the user guesses incorrectly:
if (input != computer) {
// Regardless of outcome, number of tries will increase by 1
numberOfTries = numberOfTries + 1;
// Computing the guess rate
correctGuessesRate = correctGuesses + "/" + numberOfTries;
myOutputValue =
"You guessed wrongly! You picked " +
input +
" and the computer picked " +
computer +
"!" +
"<br>" +
"<br>" +
"Number of correct guesses obtained = " +
correctGuesses +
"<br>" +
"<br>" +
"Number of correct guesses required = " +
(2 - correctGuesses) +
"<br>" +
"<br>" +
"Your guess rate = " +
correctGuessesRate +
"<br>" +
"<br>" +
"Try again by inputting 'banana', 'chisel' or 'faucet'.";
console.log("The results are different");
}
// If the user guesses correctly
if (input == computer) {
numberOfTries = numberOfTries + 1;
// Number of correct guesses has increased.
correctGuesses = correctGuesses + 1;
correctGuessesRate = correctGuesses + "/" + numberOfTries;
myOutputValue =
"You guessed correctly! You picked " +
input +
" and the computer picked " +
computer +
"!" +
"<br>" +
"<br>" +
"Number of correct guesses obtained = " +
correctGuesses +
"<br>" +
"<br>" +
"Number of correct guesses required = " +
(2 - correctGuesses) +
"<br>" +
"<br>" +
"Your guess rate = " +
correctGuessesRate +
"<br>" +
"<br>" +
"Try again by inputting 'banana', 'chisel' or 'faucet'.";
console.log("The results are similar");
}
if (correctGuesses >= 2.1) {
myOutputValue =
"You have already beaten the game! Refresh the site to start a new game.";
console.log("Number of correct guesses");
console.log(correctGuesses);
}
if (correctGuesses == 2) {
correctGuessesRate = correctGuesses + "/" + numberOfTries;
myOutputValue =
"You guessed correctly! You picked " +
input +
" and the computer picked " +
computer +
"!" +
"<br>" +
"<br>" +
"Number of correct guesses obtained = " +
correctGuesses +
"<br>" +
"<br>" +
"Number of correct guesses required = " +
(2 - correctGuesses) +
"<br>" +
"<br>" +
"Your guess rate = " +
correctGuessesRate +
"<br>" +
"<br>" +
"You have guessed twice correctly and hence beat the game!";
console.log("beat twice");
correctGuesses = 2.1;
}
if (input != "banana" && input != "chisel" && input != "faucet") {
myOutputValue = "Please input 'banana', 'chisel' or 'faucet'.";
}
return myOutputValue;
};
// Randomly generated computer word
var computerWord = function () {
var randomNumber = Math.random() * 3;
var randomInteger = Math.floor(randomNumber);
var randomRoll = randomInteger + 1;
console.log("This is the random number");
console.log(randomRoll);
var randomComputerWord = randomRoll;
if (randomComputerWord == 1) {
randomComputerWord = "banana";
}
if (randomComputerWord == 2) {
randomComputerWord = "chisel";
}
if (randomComputerWord == 3) {
randomComputerWord = "faucet";
}
console.log("This is the random word");
console.log(randomComputerWord);
return randomComputerWord;
};