Skip to content

Instantly share code, notes, and snippets.

@ValentynaGorbachenko
Created October 4, 2016 21:31
Show Gist options
  • Save ValentynaGorbachenko/f07428d70aeec90e752b071ae390473e to your computer and use it in GitHub Desktop.
Save ValentynaGorbachenko/f07428d70aeec90e752b071ae390473e to your computer and use it in GitHub Desktop.
RockPaperScissors created by ValentynaGorbachenko - https://repl.it/DoVZ/0
var computerChoiceFunc = function(){
var compChoice = Math.random();
if (compChoice < 0.34) {
compChoice = "rock";
} else if(compChoice <= 0.67) {
compChoice = "paper";
} else {
compChoice = "scissors";
}
console.log("computer: "+ compChoice);
return compChoice;
}
var computerChoice = computerChoiceFunc();
/*
var checkChoice = function(choice){
var tempChoice = choice.toLowerCase();
if (tempChoice!=="rock" && tempChoice!=="paper" && tempChoice!=="scissors") {
tempChoice = checkChoice(prompt("You chose "+ choice +"! Please choose rock, paper or scissors again?"));
}
console.log("user: "+ tempChoice);
return tempChoice;
}
*/
var checkChoice = function(choice){
var tempChoice = choice.toLowerCase();
if (tempChoice!=="rock" && tempChoice!=="paper" && tempChoice!=="scissors"){
do {
tempChoice = prompt("You chose "+ tempChoice +"! Please choose rock, paper or scissors again?");
} while (tempChoice!=="rock" && tempChoice!=="paper" && tempChoice!=="scissors");
}
console.log("user: "+ tempChoice);
return tempChoice;
}
var userChoice = checkChoice(prompt("Do you choose rock, paper or scissors?"));
var rulesCompare = function(choice1, choice2){
if(choice1==="rock") {
if (choice2==="scissors"){
return "rock wins";
} else {return "paper wins";}
} else if (choice1==="paper"){
if (choice2==="rock"){
return "paper wins";
} else {return "scissors wins";}
} else if (choice1==="scissors"){
if (choice2==="rock"){
return "rock wins";
} else {return "scissors wins";}
}
}
var compare = function(choice1, choice2){
if (choice1!=choice2) {
return rulesCompare(choice1, choice2);
} else if (choice1 === choice2) {
do {
computerChoice = computerChoiceFunc();
choice1 = computerChoice;
userChoice = checkChoice(prompt("It is a tie! Please choose rock, paper or scissors again?"));
choice2 = userChoice;
} while (computerChoice===userChoice);
return rulesCompare(choice1, choice2);
}
}
console.log(compare(computerChoice, userChoice));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment