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
76 lines (61 loc) · 2.04 KB
/
script.js
File metadata and controls
76 lines (61 loc) · 2.04 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
// Create a game where a player must correctly guess secret words to win.
// There is 1 secret word for each guess, and the computer randomly chooses that secret word from a set of 3 words: "banana", "chisel" and "faucet".
//To win the game the player must guess correctly twice in total, e.g. the player still wins if she guesses wrongly 1 or more times between her 2 correct guesses. For each guess, output all information such as the guessed word, the secret word, and how many correct guesses the player still needs until she wins.
var playerWins = 0;
var secretWord = "";
//Generate random number between 1 and 3
var rollDice = function () {
// produces a float between 0 and 3
var randomFloat = Math.random() * 3;
// take off the decimal
var resultInteger = Math.floor(randomFloat + 1);
return resultInteger;
};
var randomWins = rollDice() + 1;
var main = function (input) {
// Generate random number
var randomNumber = rollDice();
if (randomNumber == 1) {
secretWord = "banana";
}
if (randomNumber == 2) {
secretWord = "chisel";
}
if (randomNumber == 3) {
secretWord = "fauset";
}
if (input == secretWord) {
playerWins = playerWins + 1;
}
if (input != secretWord) {
playerWins = 0;
}
console.log("user guess is " + input);
console.log("secret word is " + secretWord);
console.log("player win count is " + playerWins);
console.log("random wins needed is " + randomWins);
console.log("random number is " + randomNumber);
var myOutputValue =
"The secret word is " +
secretWord +
". Your guess is " +
input +
". Your win is " +
playerWins +
". <br><br> Win streak needed is " +
randomWins;
// if user guess correctly twice in a row, then user wins!
if (playerWins == randomWins && input == secretWord) {
myOutputValue =
"You win! The secret word is " +
secretWord +
". Your guess is " +
input +
". Your win streak is " +
playerWins +
".";
playerWins = 0;
randomWins = rollDice() + 1;
}
return myOutputValue;
};