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
64 lines (58 loc) · 2.06 KB
/
script.js
File metadata and controls
64 lines (58 loc) · 2.06 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
var guessCount = 0; //initiate global count correct
var prevValue = "neutral"; //initiate previous value variable
var randGen = function () {
var randNum = Math.random() * 3; //0 up to until 3
randNum = Math.floor(randNum); //become integer 0 to 2.
randNum = randNum + 2; // integer 2 to 4.
return randNum;
};
var reqGuess = randGen(); //current random guess
var comGuess = function () {
// 0: "banana"; 1: "chisel"; 2: "faucet"
var diceRoll = Math.random() * 3; //up to until 3
diceRoll = Math.floor(diceRoll); //become integer 0 to 2.
return diceRoll;
};
var textToNum = function (Text) {
if (Text == "banana") {
return 0;
}
if (Text == "chisel") {
return 1;
}
if (Text == "faucet") {
return 2;
}
};
var comparator = function (userInput, comInput) {
console.log(userInput, comInput);
if (userInput == comInput) {
guessCount = guessCount + 1; //increase guess count
prevValue = "correct"; //track correct with a win
return "correct";
}
prevValue = "neutral"; //reset previous value
guessCount = 0; //reset
return "wrong";
};
var main = function (input) {
var input = textToNum(input.toLowerCase()); //converts text to number
var comOutput = comGuess(); //generates are random number from 0-2
var result = comparator(input, comOutput);
var myOutputValue =
result +
`. You need to guess correctly ${reqGuess}. You have accumulated ${guessCount} of wins in a row`;
console.log("# of Wins: " + guessCount);
console.log("preValue: " + prevValue);
console.log("reqGuess: " + reqGuess);
if (guessCount == reqGuess && prevValue == "correct") {
myOutputValue = `Congrats you won! You have guessed correctly ${guessCount} times! Game reset. You have accumulated ${guessCount} wins in a row.`;
reqGuess = randGen(); //random guess reset
}
return myOutputValue;
};
// Functional Breakdown:
// 1. Computer randomly chooses word (DONE)
// 2. Take user input into number to compare with (1) (DONE)
// 3. Check that player guess correctly twice (DONE)
// 4. Store global variable on the guess count correctly. (DONE)