From 0427f9869f44ad768e1c9d71dea10158a915f98c Mon Sep 17 00:00:00 2001 From: Eunice321 Date: Sat, 28 Aug 2021 14:31:53 +0800 Subject: [PATCH 1/2] Secret word --- script.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..40e1b62 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,32 @@ +var prevScore = 0; + var main = function (input) { - var myOutputValue = 'hello world'; + var randomWord = diceRoll(); + console.log(`secret = ${randomWord}`); + + var myOutputValue = `You guessed ${input}. The secret word is ${randomWord}. You Lose! Your score is ${prevScore}.`; + + if (input == randomWord) { + prevScore = prevScore + 1; + myOutputValue = `You guessed ${input}. The secret word is ${randomWord}. You Win! Your score is ${prevScore}`; + if (prevScore >= 2) { + myOutputValue = `You guessed ${input}. The secret word is ${randomWord}. You have officially won the game!!`; + } + } 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"; + } +}; From 81c495b320fdc89643518d248658aa6a30a25b12 Mon Sep 17 00:00:00 2001 From: Eunice321 Date: Sat, 28 Aug 2021 15:01:12 +0800 Subject: [PATCH 2/2] Secret Word Twice in a Row --- script.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/script.js b/script.js index 40e1b62..8e3f84b 100644 --- a/script.js +++ b/script.js @@ -1,16 +1,25 @@ var prevScore = 0; +var previousWord = 0; var main = function (input) { + // generate random word var randomWord = diceRoll(); - console.log(`secret = ${randomWord}`); + 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) { - prevScore = prevScore + 1; - myOutputValue = `You guessed ${input}. The secret word is ${randomWord}. You Win! Your score is ${prevScore}`; - if (prevScore >= 2) { - myOutputValue = `You guessed ${input}. The secret word is ${randomWord}. You have officially won the game!!`; + // 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; @@ -30,3 +39,8 @@ var diceRoll = function () { return "faucet"; } }; + +// a function to display previous word +var getPreviousWord = function () { + return previousWord; +};