Skip to content

Instantly share code, notes, and snippets.

@MrIee
Created December 7, 2015 05:49
Show Gist options
  • Save MrIee/0db945eb36d1c5e9d078 to your computer and use it in GitHub Desktop.
Save MrIee/0db945eb36d1c5e9d078 to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////
/* Provided Code - Please Don't Edit */
////////////////////////////////////////////////
'use strict';
function getInput() {
console.log("Please choose either 'rock', 'paper', or 'scissors'.")
return prompt();
}
function randomPlay() {
var randomNumber = Math.random();
if (randomNumber < 0.33) {
return "rock";
} else if (randomNumber < 0.66) {
return "paper";
} else {
return "scissors";
}
}
////////////////////////////////////////////////
/* Write Your Code Below */
////////////////////////////////////////////////
function getPlayerMove(move) {
// Write an expression that operates on a variable called `move`
// If a `move` has a value, your expression should evaluate to that value.
// However, if `move` is not specified / is null, your expression should equal `getInput()`.
return move || getInput();
}
function getComputerMove(move) {
// Write an expression that operates on a variable called `move`
// If a `move` has a value, your expression should evaluate to that value.
// However, if `move` is not specified / is null, your expression should equal `randomPlay()`.
return move || randomPlay();
}
function getWinner(playerMove,computerMove) {
var winner;
// Write code that will set winner to either 'player', 'computer', or 'tie' based on the values of playerMove and computerMove.
// Assume that the only values playerMove and computerMove can have are 'rock', 'paper', and 'scissors'.
// The rules of the game are that 'rock' beats 'scissors', 'scissors' beats 'paper', and 'paper' beats 'rock'.
switch (playerMove) {
case "rock":
if (computerMove == "scissors") {
winner = "player";
}
else if (computerMove == "paper") {
winner = "computer";
}
else {
winner = "tie";
}
break;
case "paper":
if (computerMove == "rock") {
winner = "player";
}
else if (computerMove == "scissors") {
winner = "computer";
}
else {
winner = "tie";
}
break;
case "scissors":
if (computerMove == "rock") {
winner = "computer";
}
else if (computerMove == "paper") {
winner = "player";
}
else {
winner = "tie";
}
break;
default:
winner = "tie";
}
return winner;
}
function playToFive() {
console.log("Let's play Rock, Paper, Scissors");
var playerWins = 0;
var computerWins = 0;
// Write code that plays 'Rock, Paper, Scissors' until either the player or the computer has won five times.
// This function should continue to play Rock Paper Scissors until either the
// player or the computer has won five times.
// After each 'round', display some text in the console indicating who played
// what, who won, and what the current scoreboard looks like.
// For example,
// console.log('Player chose ' + playerMove + ' while Computer chose ' + computerMove);
// console.log('The score is currently ' + playerWins + ' to ' + computerWins + '\n');
var playerMove = "";
var computerMove = "";
var winner = "";
while (playerWins < 5 || computerWins < 5) {
playerMove = getPlayerMove(getInput());
computerMove = getComputerMove(randomPlay());
winner = getWinner(playerMove, computerMove);
if (winner == "player") {
playerWins++;
}
else if (winner == "computer") {
computerWins++;
}
console.log("Player chose " + playerMove);
console.log("Computer chose " + computerMove);
console.log('The score is currently ' + playerWins + ' to ' + computerWins + '\n');
}
return [playerWins, computerWins];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment