From e06afcdc98665f5039cbd8c1f7ff37ef86b25ee6 Mon Sep 17 00:00:00 2001 From: michelle-chandiari Date: Thu, 14 Oct 2021 11:45:41 +0800 Subject: [PATCH 1/3] I edited the script code with my own code --- script.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..c74d986 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,63 @@ var main = function (input) { - var myOutputValue = 'hello world'; + // input validation check + if (!(input == "scissors" || input == "paper" || input == "stone")) { + var myOutputValue = `sorry, there are only 3 input options. please try again.`; + } + console.log(`user chooses `); + console.log(input); + + var randomNumber1 = rollDice(); + if (randomNumber1 == 1) { + randomNumber1 = "scissors"; + } + + if (randomNumber1 == 2) { + randomNumber1 = "paper"; + } + + if (randomNumber1 == 3) { + randomNumber1 = "stone"; + } + console.log(`program chooses `); + console.log(randomNumber1); + + // scenarios where user wins + // use case: if user's input equals scissors, but program 'chooses' paper, + // use case: if user's input equals paper, but program 'chooses' stone, + // use case: if user's input equals stone, but program 'chooses' scissors, + if ( + (input == "scissors" && randomNumber1 == "paper") || + (input == "paper" && randomNumber1 == "stone") || + (input == "stone" && randomNumber1 == "scissors") + ) { + myOutputValue = `you won`; + } + + //scenarios where it's a draw + // use case: if user's input equals to program's input -> draw + else if (randomNumber1 == input) { + myOutputValue = `it's a draw`; + } + // scenarios where program wins + // use case: if program's input equals scissors, but user 'chooses' paper, + // use case: if program's input equals paper, but user 'chooses' stone, + // use case: if program's input equals stone, but user 'chooses' scissors, + else if ( + (randomNumber1 == "scissors" && input == "paper") || + (randomNumber1 == "paper" && input == "stone") || + (randomNumber1 == "stone" && input == "scissors") + ) { + myOutputValue = `you lose, the program won`; + } + + // Return output return myOutputValue; }; +// function to generate 3 random numbers +var rollDice = function () { + var randomDecimal = Math.random() * 3; + var randomInteger = Math.floor(randomDecimal); + var randomNumber = randomInteger + 1; + + return randomNumber; +}; From 56401e1b24dcbcf4999d61f8713e5dc38790cdfd Mon Sep 17 00:00:00 2001 From: michelle-chandiari Date: Sun, 17 Oct 2021 15:01:58 +0800 Subject: [PATCH 2/3] edited the code to add win loss record, user name and new output formats --- script.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index c74d986..9cb00fe 100644 --- a/script.js +++ b/script.js @@ -1,7 +1,14 @@ +var userWinCount = 0; +var programWinCount = 0; +var drawCount = 0; +var userName = ""; + var main = function (input) { // input validation check if (!(input == "scissors" || input == "paper" || input == "stone")) { - var myOutputValue = `sorry, there are only 3 input options. please try again.`; + // var myOutputValue = `sorry, there are only 3 input options. please try again.`; + userName = input; + var myOutputValue = `Hi there ${input}, let's start the first round. Please input 'scissors', 'paper' or 'stone'.`; } console.log(`user chooses `); console.log(input); @@ -30,13 +37,15 @@ var main = function (input) { (input == "paper" && randomNumber1 == "stone") || (input == "stone" && randomNumber1 == "scissors") ) { - myOutputValue = `you won`; + userWinCount = userWinCount + 1; + myOutputValue = `You're doing great, ${userName}.
You won ${userWinCount} time`; } //scenarios where it's a draw // use case: if user's input equals to program's input -> draw else if (randomNumber1 == input) { - myOutputValue = `it's a draw`; + drawCount = drawCount + 1; + myOutputValue = `it's a draw, ${userName}.
Total number of draw is ${drawCount} time`; } // scenarios where program wins // use case: if program's input equals scissors, but user 'chooses' paper, @@ -47,7 +56,8 @@ var main = function (input) { (randomNumber1 == "paper" && input == "stone") || (randomNumber1 == "stone" && input == "scissors") ) { - myOutputValue = `you lose, the program won`; + programWinCount = programWinCount + 1; + myOutputValue = `the program won.
You lose ${programWinCount} time, ${userName}.`; } // Return output From 6d224e198c353fb29e03c4e770f619b007e9eb2b Mon Sep 17 00:00:00 2001 From: michelle-chandiari Date: Thu, 21 Oct 2021 16:41:15 +0800 Subject: [PATCH 3/3] edited the code to store username until program restarts --- script.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/script.js b/script.js index 9cb00fe..042d4b4 100644 --- a/script.js +++ b/script.js @@ -4,11 +4,23 @@ var drawCount = 0; var userName = ""; var main = function (input) { - // input validation check - if (!(input == "scissors" || input == "paper" || input == "stone")) { - // var myOutputValue = `sorry, there are only 3 input options. please try again.`; + // on first run, anything that user writes shall be the username and it will be stored permanently + + // first run: true true + // second run: false true + if (userName.length == 0 && input != null) { userName = input; var myOutputValue = `Hi there ${input}, let's start the first round. Please input 'scissors', 'paper' or 'stone'.`; + return myOutputValue; + } + + // input validation check + if ( + !(input == "scissors" || input == "paper" || input == "stone") && + userName.length !== 0 + ) { + var myOutputValue = `sorry, there are only 3 input options. Please try again with 'scissors', 'paper' or 'stone'.`; + return myOutputValue; } console.log(`user chooses `); console.log(input);