-
Notifications
You must be signed in to change notification settings - Fork 348
nat 11-6 part 2 #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
nat 11-6 part 2 #345
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,79 @@ | ||
| //include current gamemode | ||
| var currentGameMode = 'insert username to begin'; | ||
| //include option for username | ||
| var userName = ''; | ||
| // track number of games user wins | ||
| var numUserWin = 0; | ||
| // track number of games user lost | ||
| var numUserLost = 0; | ||
| //track number of games user draws | ||
| var numUserDraw = 0; | ||
|
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good use of global variables to keep track of different parameters. Only required to keep track of numUserWin, but implementation of numUserLost and numUserDraw shows understanding of concept of variable value persistency over a program's lifecycle. |
||
|
|
||
| var main = function (input) { | ||
| var myOutputValue = 'hello world'; | ||
| // first game mode is when username is keyed in | ||
| if (currentGameMode == "insert username to begin") { | ||
| userName = input; | ||
| currentGameMode = "commence scissors paper stone game"; | ||
| myOutputValue = "hello " + input + " you can now begin the game."; | ||
| // second game mode is when username is not keyed in | ||
| } else if (!input) { | ||
| currentGameMode = "insert username to begin"; | ||
| userName = !input; | ||
| myOutputValue = "error please insert username to begin"; | ||
| } | ||
| return myOutputValue; | ||
| }; | ||
|
Comment on lines
12
to
25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is another main() from line 54 onwards below. There cannot be 2 functions with the same name. When main() is invoked, computer will not know which main() to use. |
||
|
|
||
| // assign variables to scissors, paper, stone | ||
| var SCISSORS = 'scissors'; | ||
| var PAPER = 'paper'; | ||
| var STONE = 'stone'; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should name these variables as below to follow javascript naming convention of camelCase. |
||
| // get a random whole number between 0 to 2 | ||
| var getRandomInteger = function () { | ||
| var randomDecimal = Math.random() * 3; | ||
| var randomNum = Math.floor(randomDecimal); | ||
| return randomNum; | ||
| } | ||
|
|
||
| // assign random number to scissors, paper, or stone | ||
| var genComputerChoice = function (randomNum) { | ||
| if (randomNum == 0) { | ||
| return SCISSORS; | ||
| } | ||
| if (randomNum == 1) { | ||
| return PAPER; | ||
| } | ||
| if (randomNum == 2) { | ||
| return STONE; | ||
| } | ||
|
|
||
| // invoke functions | ||
| var computerChoice = genComputerChoice(getRandomInteger()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. var computerChoice needs to be inside main() so it is called ea time main() is called, so a diff computerChoice can be created ea time. Otherwise, computerChoice is only created once, and computer will always play the same symbol... |
||
|
|
||
| var main = function (input) { | ||
| // user input scissors, computer chooses paper | ||
| if (input == SCISSORS && computerChoice == PAPER) { | ||
| numUserWin = numUserWin + 1; | ||
| myOutputValue = `you win because your ${input} beat the computer's ${computerChoice} and you have won a total of ${numUserWin}`; | ||
| console.log(computerChoice); | ||
| console.log("the computer drew paper"); | ||
| } | ||
| // user input scissors, computer chooses stone | ||
| if (input == SCISSORS && computerChoice == STONE) { | ||
| // insert loss counter | ||
| numUserLost = numUserLost + 1; | ||
| myOutputValue = `you lose because the computer's ${computerChoice} beat your ${input}`; | ||
| console.log(computerChoice); | ||
| console.log("the computer drew stone"); | ||
| } | ||
| // user input scissors, computer chooses scissors | ||
| if (input == SCISSORS && computerChoice == SCISSORS) { | ||
| // insert draw counter | ||
| numUserDraw = numUserDraw + 1; | ||
| myOutputValue = `draw! your ${input} is the same as the computer's ${computerChoice} and you have drawn a total of ${numUserDraw}`; | ||
| console.log(computerChoice); | ||
| console.log("the computer drew scissors"); | ||
| } | ||
| return myOutputValue; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dun actually need this variable. Can test if userName=="" instead to decide if should assign input as userName instead of using it as input to play scissors paper stone.