From 1a5ff22f9bb091b13f42b488ee5c6cd93abe39bb Mon Sep 17 00:00:00 2001 From: davemeng Date: Sat, 16 Oct 2021 18:19:11 +0800 Subject: [PATCH 1/3] secret word --- script.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index bbe8a29..ac026ad 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,94 @@ +var winRecord = 0; var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; + // Generate a random dice number + var randomDiceNumber = rollDice(); + // Assign a play to each dice number + if (randomDiceNumber == 0) { + var computerTurn = "banana"; + console.log("computer played 0 - banana"); + console.log(randomDiceNumber == 0); + } + + if (randomDiceNumber == 1) { + console.log("computer played 1 - chiesel"); + console.log(randomDiceNumber == 1); + var computerTurn = "chiesel"; + } + + if (randomDiceNumber == 2) { + console.log("computer played 2 - faucet"); + console.log(randomDiceNumber == 2); + var computerTurn = "faucet"; + } + + // Assign an outcome based on each input + // Input Validation + if (input != "chiesel" && input != "banana" && input != "faucet") { + myOutputValue = + "You played " + + input + + " which is invalid, please only play chiesel, banana or faucet."; + // Return output. + console.log("input validation: ", input); + return myOutputValue; + } + + // If user guesses incorrect + if (input != computerTurn) { + myOutputValue = + "You played " + + input + + " and the computer played " + + computerTurn + + " so you lose and your win record is " + + winRecord + + " which means you need " + + (2 - winRecord) + + " more correct guess to win"; + // Return output. + console.log("you played ", input, " and computer played ", computerTurn); + return myOutputValue; + } + + // If user guesses correctly twice + if (input == computerTurn && winRecord == 1) { + winRecord = winRecord + 1; + myOutputValue = + "You played " + + input + + " and the computer played " + + computerTurn + + " so you win because your winRecord is " + + winRecord; + // Return output. + console.log("you played ", input, " and computer played ", computerTurn); + winRecord = winRecord - 2; + return myOutputValue; + } + + // If user guesses correctly once + if (input == computerTurn && winRecord == 0) { + winRecord = winRecord + 1; + myOutputValue = + "You played " + + input + + " and the computer played " + + computerTurn + + " so you need " + + (2 - winRecord) + + " more correct guess to win"; + // Return output. + console.log("you played ", input, " and computer played ", computerTurn); + return myOutputValue; + } +}; + +var rollDice = function () { + // Generate a decimal from 0 through 3, inclusive of 0 + var randomDecimal = Math.random() * 3; + // Remove the decimal with the floor operation. + // This will be an integer from 0 to 2 inclusive. + var randomInteger = Math.floor(randomDecimal); + console.log("dice rolls: ", randomInteger); + return randomInteger; }; From e47ad7ffab7bbe144a2227ad51d8298a5e4ea7f4 Mon Sep 17 00:00:00 2001 From: davemeng Date: Sat, 16 Oct 2021 18:29:05 +0800 Subject: [PATCH 2/3] secret word twice in a row --- script.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index ac026ad..f101e11 100644 --- a/script.js +++ b/script.js @@ -35,6 +35,7 @@ var main = function (input) { // If user guesses incorrect if (input != computerTurn) { + winRecord = 0; myOutputValue = "You played " + input + @@ -58,7 +59,7 @@ var main = function (input) { input + " and the computer played " + computerTurn + - " so you win because your winRecord is " + + " so you win because your win record is " + winRecord; // Return output. console.log("you played ", input, " and computer played ", computerTurn); @@ -74,7 +75,7 @@ var main = function (input) { input + " and the computer played " + computerTurn + - " so you need " + + " so you win and you need " + (2 - winRecord) + " more correct guess to win"; // Return output. From 8e562c3434159066d38023ebcc7c7ce2d2854e72 Mon Sep 17 00:00:00 2001 From: davemeng Date: Sat, 16 Oct 2021 18:55:54 +0800 Subject: [PATCH 3/3] Secret Word X in a Row --- script.js | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/script.js b/script.js index f101e11..de085e0 100644 --- a/script.js +++ b/script.js @@ -1,7 +1,21 @@ +var rollDice = function () { + // Generate a decimal from 0 through 3, inclusive of 0 but not 1 + var randomDecimal = Math.random() * 3; + // Remove the decimal with the floor operation. + // This will be an integer from 0 to 2 inclusive. + var randomInteger = Math.floor(randomDecimal); + return randomInteger; +}; + var winRecord = 0; +// Generate a random dice number for required guesses in row +var guessesInRow = rollDice() + 2; + var main = function (input) { - // Generate a random dice number + // Generate a random dice number for roll var randomDiceNumber = rollDice(); + console.log("number generated for computer play: ", randomDiceNumber); + console.log("guesses in a row required: ", guessesInRow); // Assign a play to each dice number if (randomDiceNumber == 0) { var computerTurn = "banana"; @@ -21,7 +35,7 @@ var main = function (input) { var computerTurn = "faucet"; } - // Assign an outcome based on each input + // Assign an output based on each input // Input Validation if (input != "chiesel" && input != "banana" && input != "faucet") { myOutputValue = @@ -35,7 +49,6 @@ var main = function (input) { // If user guesses incorrect if (input != computerTurn) { - winRecord = 0; myOutputValue = "You played " + input + @@ -44,15 +57,15 @@ var main = function (input) { " so you lose and your win record is " + winRecord + " which means you need " + - (2 - winRecord) + + (guessesInRow - winRecord) + " more correct guess to win"; // Return output. console.log("you played ", input, " and computer played ", computerTurn); return myOutputValue; } - // If user guesses correctly twice - if (input == computerTurn && winRecord == 1) { + // If user guesses correctly the required times + if (input == computerTurn && winRecord == guessesInRow - 1) { winRecord = winRecord + 1; myOutputValue = "You played " + @@ -63,12 +76,14 @@ var main = function (input) { winRecord; // Return output. console.log("you played ", input, " and computer played ", computerTurn); - winRecord = winRecord - 2; + // Reset win record and number of guesses required + winRecord = 0; + guessesInRow = rollDice() + 2; return myOutputValue; } // If user guesses correctly once - if (input == computerTurn && winRecord == 0) { + if (input == computerTurn && winRecord < guessesInRow) { winRecord = winRecord + 1; myOutputValue = "You played " + @@ -76,20 +91,10 @@ var main = function (input) { " and the computer played " + computerTurn + " so you win and you need " + - (2 - winRecord) + + (guessesInRow - winRecord) + " more correct guess to win"; // Return output. console.log("you played ", input, " and computer played ", computerTurn); return myOutputValue; } }; - -var rollDice = function () { - // Generate a decimal from 0 through 3, inclusive of 0 - var randomDecimal = Math.random() * 3; - // Remove the decimal with the floor operation. - // This will be an integer from 0 to 2 inclusive. - var randomInteger = Math.floor(randomDecimal); - console.log("dice rolls: ", randomInteger); - return randomInteger; -};