From f084120adb050fdff59500d0f47acd22f14db2e3 Mon Sep 17 00:00:00 2001 From: 0besitin Date: Wed, 13 Oct 2021 20:43:50 +0800 Subject: [PATCH 1/3] first draft of game --- script.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..a0434da 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,68 @@ var main = function (input) { - var myOutputValue = 'hello world'; + var myOutputValue = "hello world"; + + var computerShows = computerGuess(); + //if user chooses rock, computer chooses scissors, user wins + //if user chooses scissors, computer chooses paper, user wins + //if user chooses paper, computer chooses rock, user wins + if ( + (input == "rock" && computerShows == "scissors") || + (input == "paper" && computerShows == "rock") || + (input == "scissors" && computerShows == "paper") + ) { + var myOutputValue = + "you win!

you guessed " + + input + + " while computer guessed " + + computerShows; + } + + //if user chooses rock, computer chooses paper, user loses + //if user chooses scissors, computer chooses rock, user loses + //if user chooses paper, computer chooses scissors, user loses + if ( + (input == "rock" && computerShows == "paper") || + (input == "paper" && computerShows == "scissors") || + (input == "scissors" && computerShows == "rock") + ) { + var myOutputValue = + "you lose!

you guessed " + + input + + " while computer guessed " + + computerShows; + } + + //if user chooses rock, computer chooses rock, they draw + //if user chooses scissors, computer chooses scissors, they draw + //if user chooses paper, computer chooses paper, they draw + if ( + (input == "rock" && computerShows == "rock") || + (input == "paper" && computerShows == "paper") || + (input == "scissors" && computerShows == "scissors") + ) { + var myOutputValue = + "its a draw!

you guessed " + + input + + " while computer guessed " + + computerShows; + } + return myOutputValue; }; + +//computer will generate either rock, scissors or paper +var computerGuess = function () { + var randomDecimal = Math.random(); + var randomNumber = Math.floor(randomDecimal * 3); + var randomNumberPlusOne = randomNumber + 1; + if (randomNumberPlusOne == 1) { + var computerOutput = "rock"; + } + if (randomNumberPlusOne == 2) { + var computerOutput = "paper"; + } + if (randomNumberPlusOne == 3) { + var computerOutput = "scissors"; + } + return computerOutput; +}; From 7d9429a4e25308051cc5eef082a90a097090fd4f Mon Sep 17 00:00:00 2001 From: 0besitin Date: Sat, 16 Oct 2021 16:19:23 +0800 Subject: [PATCH 2/3] added win-loss --- script.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/script.js b/script.js index a0434da..4146c38 100644 --- a/script.js +++ b/script.js @@ -1,5 +1,9 @@ +//declare global variable +var winCounter = 0; +var loseCounter = 0; + var main = function (input) { - var myOutputValue = "hello world"; + var myOutputValue = "Please input Rock, Paper or Scissors"; var computerShows = computerGuess(); //if user chooses rock, computer chooses scissors, user wins @@ -10,11 +14,21 @@ var main = function (input) { (input == "paper" && computerShows == "rock") || (input == "scissors" && computerShows == "paper") ) { + winCounter = winCounter + 1; + var winPercentage = ( + 100 * + (winCounter / (winCounter + loseCounter)) + ).toFixed(2); + console.log("winCount"); + console.log(winCounter); var myOutputValue = "you win!

you guessed " + input + " while computer guessed " + - computerShows; + computerShows + + "
you won " + + winPercentage + + " % of the time"; } //if user chooses rock, computer chooses paper, user loses @@ -25,11 +39,21 @@ var main = function (input) { (input == "paper" && computerShows == "scissors") || (input == "scissors" && computerShows == "rock") ) { + loseCounter = loseCounter + 1; + console.log("loseCount"); + console.log(loseCounter); + var winPercentage = ( + 100 * + (winCounter / (winCounter + loseCounter)) + ).toFixed(2); var myOutputValue = "you lose!

you guessed " + input + " while computer guessed " + - computerShows; + computerShows + + "
you won " + + winPercentage + + " % of the time"; } //if user chooses rock, computer chooses rock, they draw From 9a43a887066ded13532afa67cbbf74dbdb601b0d Mon Sep 17 00:00:00 2001 From: 0besitin Date: Mon, 18 Oct 2021 16:16:27 +0800 Subject: [PATCH 3/3] added named, changed win loss record display --- script.js | 57 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/script.js b/script.js index 4146c38..437ccf0 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,23 @@ +//prompt for name +var user = prompt("Please enter your name"); + +if (user == null) { + var user = prompt("No name recorded. Please enter your name"); +} + +if (user != null) { + alert( + "Hi " + + user + + "!" + + " Please input Rock, Paper or Scissors in the Input Box to start playing" + ); +} + //declare global variable var winCounter = 0; var loseCounter = 0; +var totalTries = 0; var main = function (input) { var myOutputValue = "Please input Rock, Paper or Scissors"; @@ -19,16 +36,20 @@ var main = function (input) { 100 * (winCounter / (winCounter + loseCounter)) ).toFixed(2); + totalTries = totalTries + 1; console.log("winCount"); console.log(winCounter); var myOutputValue = - "you win!

you guessed " + + user + + " wins!

You guessed " + input + - " while computer guessed " + + " while Computer guessed " + computerShows + - "
you won " + - winPercentage + - " % of the time"; + "
Out of " + + totalTries + + " tries, you have won " + + winCounter + + " times"; } //if user chooses rock, computer chooses paper, user loses @@ -46,14 +67,18 @@ var main = function (input) { 100 * (winCounter / (winCounter + loseCounter)) ).toFixed(2); + totalTries = totalTries + 1; var myOutputValue = - "you lose!

you guessed " + + user + + " loses!

You guessed " + input + - " while computer guessed " + + " while Computer guessed " + computerShows + - "
you won " + - winPercentage + - " % of the time"; + "
Out of " + + totalTries + + " tries, you have won " + + winCounter + + " times"; } //if user chooses rock, computer chooses rock, they draw @@ -64,11 +89,17 @@ var main = function (input) { (input == "paper" && computerShows == "paper") || (input == "scissors" && computerShows == "scissors") ) { + totalTries = totalTries + 1; var myOutputValue = - "its a draw!

you guessed " + + "It's a draw!

You guessed " + input + - " while computer guessed " + - computerShows; + " while Computer guessed " + + computerShows + + "
Out of " + + totalTries + + " tries, you have won " + + winCounter + + " times"; } return myOutputValue;