From e0a8c286a0f001862f189a8f6e224fac02fea04a Mon Sep 17 00:00:00 2001 From: Linwt94 Date: Thu, 14 Oct 2021 20:42:46 +0800 Subject: [PATCH 1/2] Created Secret Word Game --- script.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index bbe8a29..6726144 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,53 @@ +var randomWordGenerate = function () { + var randomDecimal = Math.random() * 3; + var randomInteger = Math.floor(randomDecimal) + 1; + + var randomWord = 0; + + if (randomInteger == 1) { + randomWord = "banana"; + } + + if (randomInteger == 2) { + randomWord = "chisel"; + } + + if (randomInteger == 3) { + randomWord = "faucet"; + } + return randomWord; +}; + +var winCount = 0; + var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; + var secretWord = randomWordGenerate(); + //var secretWord = "banana"; + + var myOutputValue = "You lose. You still need 2 correct guesses"; + + console.log("secret word ===", secretWord); + + if (winCount == 1) { + myOutputValue = + "That was not the secret word. You now have 1 correct guess. Guess it correct again to win."; + } + + if (input == secretWord) { + winCount += 1; + myOutputValue = "You have 1 correct guess. Guess it correct again to win."; + } + + if (winCount == 2) { + myOutputValue = "You have 2 correct guesses. You win!"; + } + + return ( + "You guessed " + + input + + ". The secret word was " + + secretWord + + ". " + + myOutputValue + ); }; From f958806e81b2bfebf20cc915824390980e81d7ca Mon Sep 17 00:00:00 2001 From: Linwt94 Date: Thu, 14 Oct 2021 21:26:19 +0800 Subject: [PATCH 2/2] Amend to x round Secret Word Game --- script.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/script.js b/script.js index 6726144..c7d9430 100644 --- a/script.js +++ b/script.js @@ -1,3 +1,13 @@ +var randomWinsNeeded = function () { + var randomDecimal = Math.random() * 3; + var randomInteger = Math.floor(randomDecimal) + 2; + + return randomInteger; +}; + +var winsNeeded = randomWinsNeeded(); +console.log("correct guesses to win=====", winsNeeded); + var randomWordGenerate = function () { var randomDecimal = Math.random() * 3; var randomInteger = Math.floor(randomDecimal) + 1; @@ -21,25 +31,30 @@ var randomWordGenerate = function () { var winCount = 0; var main = function (input) { - var secretWord = randomWordGenerate(); - //var secretWord = "banana"; + //var secretWord = randomWordGenerate(); + var secretWord = "banana"; - var myOutputValue = "You lose. You still need 2 correct guesses"; + var myOutputValue = `You need ${winsNeeded} correct guesses in a row to win`; console.log("secret word ===", secretWord); - if (winCount == 1) { - myOutputValue = - "That was not the secret word. You now have 1 correct guess. Guess it correct again to win."; + if (input != secretWord) { + winCount = 0; } if (input == secretWord) { winCount += 1; - myOutputValue = "You have 1 correct guess. Guess it correct again to win."; + console.log("curren correct guesses", winCount); + myOutputValue = `You have ${winCount} correct guesses. You need need ${ + winsNeeded - winCount + } more correct guesses in a row to win`; } - if (winCount == 2) { - myOutputValue = "You have 2 correct guesses. You win!"; + if (winCount == winsNeeded) { + myOutputValue = `You guess correctly ${winsNeeded} time. You win!`; + winCount = 0; + winsNeeded = randomWinsNeeded(); + console.log("correct guesses to win in new round=====", winsNeeded); } return (