From b639ebd6817a1951b7fb08df7c85c998b46004c2 Mon Sep 17 00:00:00 2001 From: HAYLEYCHEW Date: Sat, 16 Oct 2021 15:27:01 +0800 Subject: [PATCH 1/2] completed secret word game --- script.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..ff3e500 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,40 @@ +var CorrectGuessCounter = 0; +var CorrectGuessToWin = 2; + var main = function (input) { - var myOutputValue = 'hello world'; + var SecretWord = chooseSecretWord(); + console.log("selecting SecretWord"); + console.log(SecretWord); + var myOutputValue = "You selected " + input + ". You guessed incorrectly."; + + //note that the sequence of the if functions matter as it determines the statement first + if (input == SecretWord && CorrectGuessCounter == 1) { + CorrectGuessCounter = CorrectGuessCounter + 1; + var CorrectGuessRemainingToWin = CorrectGuessToWin - CorrectGuessCounter; + console.log("counting correct guesses"); + console.log(CorrectGuessCounter); + return `You guessed correctly twice. You win.`; + } + + if (input == SecretWord) { + CorrectGuessCounter = CorrectGuessCounter + 1; + var CorrectGuessRemainingToWin = CorrectGuessToWin - CorrectGuessCounter; + console.log("counting correct guesses"); + console.log(CorrectGuessCounter); + return `You guessed correctly. You need ${CorrectGuessRemainingToWin} more correct guess to win.`; + } + return myOutputValue; }; + +var chooseSecretWord = function () { + var randomDecimal = Math.random() * 3; + var randomSecretWord = Math.floor(randomDecimal); + if (randomSecretWord == 0) { + return "banana"; + } + if (randomSecretWord == 1) { + return "chisel"; + } + return "faucet"; +}; From 6862c9b7983e8b880ec7d31daade3d18a8a529cc Mon Sep 17 00:00:00 2001 From: HAYLEYCHEW Date: Sat, 16 Oct 2021 15:35:21 +0800 Subject: [PATCH 2/2] changed the output wording --- script.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index ff3e500..fe64f21 100644 --- a/script.js +++ b/script.js @@ -5,7 +5,10 @@ var main = function (input) { var SecretWord = chooseSecretWord(); console.log("selecting SecretWord"); console.log(SecretWord); - var myOutputValue = "You selected " + input + ". You guessed incorrectly."; + var myOutputValue = + "You selected '" + + input + + "' which is not the secret word. Please try again."; //note that the sequence of the if functions matter as it determines the statement first if (input == SecretWord && CorrectGuessCounter == 1) {