From 525be8c687b3e104a666ef5d454c482ab1836a99 Mon Sep 17 00:00:00 2001 From: imkenhere Date: Tue, 20 Jul 2021 16:35:27 +0800 Subject: [PATCH 1/2] Secret Word Game --- script.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..3a36463 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,101 @@ +var correctGuesses = 0; + var main = function (input) { - var myOutputValue = 'hello world'; + var computer = computerWord(); + var myOutputValue = "Please input 'banana', 'chisel' or 'faucet'."; + + if (input != computer) { + myOutputValue = + "You guessed wrongly! You picked " + + input + + " and the computer picked " + + computer + + "!" + + "
" + + "
" + + "Number of correct guesses obtained = " + + correctGuesses + + "
" + + "
" + + "Number of correct guesses required = " + + (2 - correctGuesses) + + "
" + + "
" + + "Try again by inputting 'banana', 'chisel' or 'faucet'."; + console.log("The results are different"); + } + if (input == computer) { + correctGuesses = correctGuesses + 1; + myOutputValue = + "You guessed correctly! You picked " + + input + + " and the computer picked " + + computer + + "!" + + "
" + + "
" + + "Number of correct guesses obtained = " + + correctGuesses + + "
" + + "
" + + "Number of correct guesses required = " + + (2 - correctGuesses) + + "
" + + "
" + + "Try again by inputting 'banana', 'chisel' or 'faucet'."; + console.log("The results are similar"); + } + if (correctGuesses >= 2.1) { + myOutputValue = + "You have already beaten the game! Refresh the site to start a new game."; + console.log("Number of correct guesses"); + console.log(correctGuesses); + } + if (correctGuesses == 2) { + myOutputValue = + "You guessed correctly! You picked " + + input + + " and the computer picked " + + computer + + "!" + + "
" + + "
" + + "Number of correct guesses obtained = " + + correctGuesses + + "
" + + "
" + + "Number of correct guesses required = " + + (2 - correctGuesses) + + "
" + + "
" + + "You have guessed twice correctly and hence beat the game!"; + console.log("beat twice"); + correctGuesses = 2.1; + } + if (input != "banana" && input != "chisel" && input != "faucet") { + myOutputValue = "Please input 'banana', 'chisel' or 'faucet'."; + } return myOutputValue; }; + +// Randomly generated computer word +var computerWord = function () { + var randomNumber = Math.random() * 3; + var randomInteger = Math.floor(randomNumber); + var randomRoll = randomInteger + 1; + console.log("This is the random number"); + console.log(randomRoll); + var randomComputerWord = randomRoll; + if (randomComputerWord == 1) { + randomComputerWord = "banana"; + } + if (randomComputerWord == 2) { + randomComputerWord = "chisel"; + } + if (randomComputerWord == 3) { + randomComputerWord = "faucet"; + } + console.log("This is the random word"); + console.log(randomComputerWord); + return randomComputerWord; +}; From 308c0245f7bcaa6ab5dd48154b4a6ea5e47e6b58 Mon Sep 17 00:00:00 2001 From: imkenhere Date: Tue, 20 Jul 2021 17:01:30 +0800 Subject: [PATCH 2/2] Secret Word - Added Winrate --- script.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index 3a36463..37e7995 100644 --- a/script.js +++ b/script.js @@ -1,10 +1,18 @@ +// Winrate var correctGuesses = 0; - +var numberOfTries = 0; +var correctGuessesRate = correctGuesses + "/" + numberOfTries; +// Game var main = function (input) { var computer = computerWord(); + // Default output var myOutputValue = "Please input 'banana', 'chisel' or 'faucet'."; - + // If the user guesses incorrectly: if (input != computer) { + // Regardless of outcome, number of tries will increase by 1 + numberOfTries = numberOfTries + 1; + // Computing the guess rate + correctGuessesRate = correctGuesses + "/" + numberOfTries; myOutputValue = "You guessed wrongly! You picked " + input + @@ -21,11 +29,19 @@ var main = function (input) { (2 - correctGuesses) + "
" + "
" + + "Your guess rate = " + + correctGuessesRate + + "
" + + "
" + "Try again by inputting 'banana', 'chisel' or 'faucet'."; console.log("The results are different"); } + // If the user guesses correctly if (input == computer) { + numberOfTries = numberOfTries + 1; + // Number of correct guesses has increased. correctGuesses = correctGuesses + 1; + correctGuessesRate = correctGuesses + "/" + numberOfTries; myOutputValue = "You guessed correctly! You picked " + input + @@ -42,6 +58,10 @@ var main = function (input) { (2 - correctGuesses) + "
" + "
" + + "Your guess rate = " + + correctGuessesRate + + "
" + + "
" + "Try again by inputting 'banana', 'chisel' or 'faucet'."; console.log("The results are similar"); } @@ -52,6 +72,7 @@ var main = function (input) { console.log(correctGuesses); } if (correctGuesses == 2) { + correctGuessesRate = correctGuesses + "/" + numberOfTries; myOutputValue = "You guessed correctly! You picked " + input + @@ -68,6 +89,10 @@ var main = function (input) { (2 - correctGuesses) + "
" + "
" + + "Your guess rate = " + + correctGuessesRate + + "
" + + "
" + "You have guessed twice correctly and hence beat the game!"; console.log("beat twice"); correctGuesses = 2.1;