Rock, Paper, Scissors, Lizard, Spock Revision


#1

Okay, I modified the rock, paper, scissors code, to make it into the infamous Sheldon remake, of rock, paper, scissors, lizard, Spock. Since I did it on my own, and I'm still a beginner, it would be awesome if someone could try it out and see if there are any bugs in it. I tried it a few times, and it worked. I just want to make sure it's perfect. So, yeah. Here it is!

var userChoice = prompt("Do you choose rock, paper, scissors, lizard, or Spock?");
var computerChoice = Math.random();
if (computerChoice < 0.2) {
	computerChoice = "rock";
} else if(computerChoice <=0.4) {
	computerChoice = "paper";
} else if(computerChoice <=0.6) {
	computerChoice = "scissors";
} else if(computerChoice <=0.8) {
    computerChoice = "lizard";
} else {
    computerChoice = "Spock";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2){
    if (choice1===choice2){
        return "It is a draw. Fight to the death.";
    } else if (choice1==="rock"){
        if (choice2==="scissors"){
            return "rock wins";
        } else if (choice2==="paper"){
            return "paper wins";
        } else if (choice2==="lizard"){
            return "rock wins";
        } else {
            return "Spock wins"
        }
    } else if (choice1==="paper"){
        if (choice2==="rock"){
            return "paper wins";
        } else if (choice2==="scissors"){
            return "scissors wins";
        } else if (choice2==="lizard"){
            return "lizard wins";
        } else {
            return "paper wins";
        }
    } else if (choice1==="scissors"){
        if (choice2==="paper"){
            return "scissors wins";
        } else if (choice2==="rock"){
            return "rock wins";
        } else if (choice2==="lizard"){
            return "scissors wins";
        } else {
            return "Spock wins";
        }
    } else if (choice1==="lizard"){
        if (choice2==="rock"){
            return "rock wins";
        } else if (choice2==="paper"){
            return "lizard wins";
        } else if (choice2==="scissors"){
            return "scissors wins";
        } else {
            return "lizard wins";
        }
    } else if (choice1==="Spock"){
        if (choice2==="rock"){
            return "Spock wins";
        } else if (choice2==="paper"){
            return "paper wins";
        } else if (choice2==="scissors"){
            return "Spock wins";
        } else {
            return "lizard wins";
        }
    }
}
compare(userChoice,computerChoice)

Edited, fixed a few errors thanks to @schamanu's comment.


#2

Okay, here we go. I tried it a few more times, and whenever one wins, it will print it out on the side. However, if there is a draw, it doesn't show up. What do I do about that? Or is there nothing I can do?


#3

I tested your program in codecademy labs and it worked flawless. Showed "It's a draw. Fight to the death" everytime there was a draw. Maybe try to reload the website.

However I found another mistake. You wrote

else if (choice2="paper"){

and

else if (choice2="lizard"){

Clearly those 2 conditions should have "===" instead of "=". Both conditions can be found after the "It's a draw" message.


#4

Thank you. I'm still getting used to that. I'm going to go reload it right now. Thank you for trying it though, and your feedback. It means a lot!