-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (40 loc) · 1.32 KB
/
script.js
File metadata and controls
46 lines (40 loc) · 1.32 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
var prevScore = 0;
var previousWord = 0;
var main = function (input) {
// generate random word
var randomWord = diceRoll();
console.log(`secret word = ${randomWord}`);
var prevWord = getPreviousWord();
// update previous word with current word
previousWord = randomWord;
console.log(`previous word = ${prevWord}`);
var myOutputValue = `You guessed ${input}. The secret word is ${randomWord}. You Lose! Your score is ${prevScore}.`;
if (input == randomWord) {
// if input is guessed correctly once
myOutputValue = `You guessed ${input}. The secret word is ${randomWord}. You need one more guess! Your score is ${prevScore}`;
// if input is guessed correctly twice in a row, score is given
if (randomWord == prevWord) {
prevScore = prevScore + 1;
myOutputValue = `You guessed ${input}. The secret word is ${randomWord}. You Win! Your score is ${prevScore}`;
}
}
return myOutputValue;
};
var diceRoll = function () {
var randomDecimal = Math.random() * 3;
var randomInteger = Math.floor(randomDecimal);
var diceNumber = randomInteger;
if (diceNumber == 0) {
return "banana";
}
if (diceNumber == 1) {
return "chisel";
}
if (diceNumber == 2) {
return "faucet";
}
};
// a function to display previous word
var getPreviousWord = function () {
return previousWord;
};