From aee1b5c0424ac184d9b60e638e4f73a24e18459b Mon Sep 17 00:00:00 2001 From: richiechia Date: Sat, 16 Oct 2021 14:14:34 +0800 Subject: [PATCH 1/2] Assignment done. --- script.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..8d8f607 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,42 @@ +var counter = 0; + var main = function (input) { - var myOutputValue = 'hello world'; + var myOutputValue = "hello world"; + + var computer_choice = secret_word(); + console.log(computer_choice); + + if (input == computer_choice) { + counter += 1; + myOutputValue = "You win! You guessed it correctly " + counter + " time(s)"; + } else { + myOutputValue = "You guessed it wrongly!"; + } + return myOutputValue; }; + +var rollRandom = function () { + var randomNumber = Math.random() * 3; + var integernumber = Math.floor(randomNumber); + var choices = integernumber + 1; + return choices; +}; + +var secret_word = function () { + var choice = rollRandom(); + var word = ""; + + if (choice == 1) { + word = "banana"; + } + + if (choice == 2) { + word = "chisel"; + } + if (choice == 3) { + word = "faucet"; + } + + return word; +}; From 6a45963775baae9ddd8146daa423ddf5e8d4a1b5 Mon Sep 17 00:00:00 2001 From: richiechia Date: Sat, 16 Oct 2021 14:42:48 +0800 Subject: [PATCH 2/2] Added Secret Word X in a Row --- script.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/script.js b/script.js index 8d8f607..5510def 100644 --- a/script.js +++ b/script.js @@ -1,16 +1,34 @@ +// Function +var random_Number = function () { + var randomNumber = Math.random() * 3; + // range from 0 to 2 + var integernumber = Math.floor(randomNumber); + // range from 2 to 4 + var number = integernumber + 2; + return number; +}; + var counter = 0; +var round = random_Number(); +console.log(round); var main = function (input) { - var myOutputValue = "hello world"; + var myOutputValue = ""; var computer_choice = secret_word(); console.log(computer_choice); if (input == computer_choice) { counter += 1; - myOutputValue = "You win! You guessed it correctly " + counter + " time(s)"; + myOutputValue = "You guessed it correctly " + counter + " time(s)"; } else { - myOutputValue = "You guessed it wrongly!"; + counter = 0; + myOutputValue = + "You guessed it wrongly! " + counter + " time(s) correctly."; + } + + if (counter == round) { + myOutputValue = `You win! You guessed it correctly ${counter} time(s)`; } return myOutputValue;