forked from rocketacademy/basics-github-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
38 lines (35 loc) · 1.43 KB
/
script.js
File metadata and controls
38 lines (35 loc) · 1.43 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
* Secret Word
*/
var numCorrectGuessesNeededToWin = 2;
var numCorrectGuessesSoFar = 0;
// Randomly return one of banana, chisel or faucet.
var generateSecretWord = function () {
// Generate random number between 1 and 3
var randomdecimal = Math.random() * 3;
var randomInteger = Math.floor (randomdecimal) + 1;
// Return the word that corresponds to the relevant number
if (randomInteger == 1) {
return 'banana';
}
if (randomInteger == 2) {
return 'chisel';
}
return 'faucet';
};
var playSecretWord = function (guessedWord) {
var secretWord = generateSecretWord();
var standardMessage = `You guessed: ${guessedWord}. Secret word: ${secretWord}.`;
if (secretWord == guessedWord) {
// "+=" (below) is just a shorter way to write <numCorrectGuessesSoFar=numCorrectGuessesSoFar+1>
numCorrectGuessesSoFar = numCorrectGuessesSoFar + 1;
if (numCorrectGuessesSoFar >= numCorrectGuessesNeededToWin) {
// Reset counter of correct guesses to restart game.
numCorrectGuessesSoFar = 0;
return `${standardMessage} You guessed twice correctly. You win! Please play again.`;
}
return `${standardMessage} You guessed correctly! You need 1 more correct guess to win.`;
}
var numCorrectGuessesRemainingToWin =
numCorrectGuessesNeededToWin - numCorrectGuessesSoFar;
return `${standardMessage} You guessed incorrectly. You need ${numCorrectGuessesRemainingToWin} more correct guesses to win.`;
};