Hello guys, so I’ve extended my code from “Rock, Paper, Scissors” to “Rock, Paper, Scissors, Lizard, Spock”. It works for me. Maybe there’s a much shorter way of writing it. I’m just too excited because it’s my first working code ever (Yea, I just started with programming). Hope this helps anyone looking to try “Rock, Paper, Scissors, Spock”
var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?");
// if user makes inappropriate choice like "dog"
if(userChoice === "dog"){
// ask user for an appropriate choice
var appropriate = prompt("Inappropriate choice! Please choose again!");
//set userChoice to appropriate
userChoice = appropriate;
}
var computerChoice = Math.random();
// Get computerChoice in random
if (computerChoice <= 0.14) {
computerChoice = "rock";
}
else if(computerChoice <= 0.37) {
computerChoice = "paper";
}
else if(computerChoice <= 0.53){
computerChoice = "scissors";
}
else if(computerChoice <= 0.75){
computerChoice = "lizard";
}
else{
computerChoice = "spock";
}
// Print computerChoice
console.log("Computer: " + computerChoice);
// Now compare both userChoice with computerChoice
var compare = function(choice1,choice2){
if (choice1 === choice2){
// Request user for new choice if equals computerChoice
var newUserChoice = prompt("Please try again!");
choice1 = newUserChoice;
return choice1;
// If userChoice is "rock"
}
else if(choice1 === "rock"){
if (choice2 === "scissors"){
return "Rock crushes Scissors";
}
else if(choice2 === "lizard"){
return "Rock crushes Lizard";
}
else if(choice2 === "paper"){
return "Paper covers Rock";
}
else{
return "Spock vaporizes Rock";
}
}
// If userChoice is "paper"
else if(choice1 === "paper"){
if (choice2 === "rock"){
return "Paper covers Rock";
}
else if(choice2 === "lizard"){
return "Lizard eats Paper";
}
else if(choice2 === "spock"){
return "Paper disproves Spock";
}
else{
return "Scissors cuts Paper";
}
}
// if userChoice is "scissors"
else if(choice1 === "scissors"){
if (choice2 === "rock"){
return "Rock crushes Scissors";
}
else if(choice2 === "lizard"){
return "Scissors decapitates Lizard";
}
else if(choice2 === "spock"){
return "Spock smashes Scissors";
}
else{
return "Scissors cuts Paper";
}
}
// if userChoice is "lizard"
else if(choice1 === "lizard"){
if(choice2 === "spock"){
return "Lizard poisons Spock;"
}
else if(choice2 === "scissors"){
return "Scissors decapitates Lizard";
}
else if(choice2 === "paper"){
return "Lizard eats Paper";
}
else{
return "Rock crushes Lizard";
}
}
// if userChoice is "spock"
else if(choice1 === "spock"){
if(choice2 === "scissors"){
return "Spock smashes Scissors";
}
else if(choice2 === "paper"){
return "Paper disproves Spock";
}
else if(choice2 === "rock"){
return "Spock vaporizes Rock";
}
else{
return "Lizard poisons Spock";
}
}
}
compare(userChoice,computerChoice);