Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,79 @@
//include current gamemode
var currentGameMode = 'insert username to begin';

Copy link
Copy Markdown

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.

//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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.
The contents of main() from line 54 below should be included in this main().


// assign variables to scissors, paper, stone
var SCISSORS = 'scissors';
var PAPER = 'paper';
var STONE = 'stone';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.
var scissors ="scissors";
var paper = "paper";
var stone = "stone";

// 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());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;
};