-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathscript.js
More file actions
113 lines (95 loc) · 4.21 KB
/
script.js
File metadata and controls
113 lines (95 loc) · 4.21 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
var random2to4 = function () {
var randomDecimal = Math.random() * 3; // Math.random() --> 0 --- <1 0.1 , 0.2121241, 0.999999999999 float 2.09999
var randomInteger = Math.floor(randomDecimal); //0,1,2
return randomInteger + 2; // returns 2,3, or 4.
};
var gameNumber = 0;
var nTimesNeeded = random2to4(); //N times needed for winning--> this starts first.
var randomNtimesNeeded = "not yet activated"; //2nd: this is for count when player wins all N times. can't start this definition anywhere locally in the main function.
var sumScore = 0;
console.log("===GLOBAL nTimesNeeded to 1st ROUND===");
console.log(nTimesNeeded);
var main = function (input) {
gameNumber = gameNumber + 1;
console.log("##### GAME NUMBER #####");
console.log(gameNumber);
console.log("===LOCAL nTimesNeeded to Win CURRENT ROUND ===");
console.log(nTimesNeeded);
var randomSecretWord = readSecretWordNmbr(randomNumber());
console.log("randomSecretWord = ");
console.log(randomSecretWord);
if (
(input.toLowerCase() == randomSecretWord &&
input.toLowerCase() == "banana") ||
(input.toLowerCase() == randomSecretWord &&
input.toLowerCase() == "chisel") ||
(input.toLowerCase() == randomSecretWord && input.toLowerCase() == "faucet")
) {
sumScore = sumScore + 1;
}
var scoreDiff = nTimesNeeded - sumScore; // This tells the player how many more correct guesses needed to win current round.
var myOutputValue = "";
//need message for when the person doesn't win 2wice in a row.
var wrongGuess = `Your guess was ${input}.<br><br> Secret Word: ${randomSecretWord}.<br><br> You need ${nTimesNeeded} correct guess/guesses in a row to win. Try again. `;
var correctGuess = `Correct guess... <br><br> Your guess was ${input}.<br> Secret Word: ${randomSecretWord}.<br><br> You need ${scoreDiff} more correct guess/guesses to win. `;
if (input.toLowerCase() == randomSecretWord && sumScore == nTimesNeeded) {
myOutputValue = outputForWin;
randomNtimesNeeded = random2to4();
}
console.log("-----> randomNtimesNeeded ------>");
console.log(randomNtimesNeeded);
var outputForWin = ` YOU WIN!<br><br>You have guessed the secret word correctly ${nTimesNeeded} times in a roll! <br><br> Your ${nmbrSuffix(
nTimesNeeded
)} guess was ${input}.<br> Secret Word: ${randomSecretWord}.<br><br>In the next round, you will need to make <br> ${randomNtimesNeeded} correct guesses in a row`;
if (input.toLowerCase() != randomSecretWord) {
myOutputValue = wrongGuess;
sumScore = 0;
}
if (input.toLowerCase() == randomSecretWord && sumScore < nTimesNeeded) {
myOutputValue = correctGuess; //because sumScore and nTimesNeeded are tied to scoreDiff, I added an extra condition for better control, such that the output is correct to the exact game situation before winning after 1st correct guess.
}
if (input.toLowerCase() == randomSecretWord && sumScore == nTimesNeeded) {
nTimesNeeded = randomNtimesNeeded; //NEXT ROUND'S N TIMES TRANSFERED TO 'nTimesNeeded'
myOutputValue = outputForWin;
sumScore = 0;
}
console.log(">>>>>> scoreDiff = nTimesNeeded - sumScore");
console.log(scoreDiff);
console.log(">>>>>> sumScore END of this guess= ");
console.log(sumScore);
console.log("nTimesNeeded to WIN CURRENT / NEXT ROUND ===");
console.log(nTimesNeeded);
return myOutputValue;
};
// TO KEEP SCORE OF THE CORRECT WORD
var randomNumber = function () {
var randomDecimal = Math.random() * 3; // Math.random() --> 0 --- <1 0.1 , 0.2121241, 0.999999999999 float 2.09999
var randomInteger = Math.floor(randomDecimal); //0,1,2
return randomInteger + 1; // 1,2,3
};
// read random Number to output random GameHand
var readSecretWordNmbr = function (secretWordIndex) {
var secretWordString = "";
if (secretWordIndex == 1) {
secretWordString = "banana";
}
if (secretWordIndex == 2) {
secretWordString = "chisel";
}
if (secretWordIndex == 3) {
secretWordString = "faucet";
}
return secretWordString; // amend to 1 string input to test.
};
var nmbrSuffix = function (nTimesNeeded) {
if (nTimesNeeded == 2) {
stNdThSuffix = "2nd";
}
if (nTimesNeeded == 3) {
stNdThSuffix = "3rd";
}
if (nTimesNeeded == 4) {
stNdThSuffix = "4th";
}
return stNdThSuffix;
};