Rock paper scissors lizard spock


#1

I cant find my mistake :frowning: can you help me debug this please? (it's WIP, dont mind the comment down below ((though help with that part is always welcome too :smile:)))
There you go:

var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?");
var computerChoice = Math.random();
if (computerChoice <= 0.19) {
	computerChoice = "rock";
} else if(computerChoice <= 0.40) {
	computerChoice = "paper";
} else if(computerChoice <= 0.60{
	computerChoice = "scissors";
} else if(computerChoice <= 0.80{
computerChoice = "lizard"
} else computerChoice = "spock"
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!"
    }
    else if(choice1 === "rock") { 
        if(choice2==="scissors") {
            return"rock wins"}
            else if(choice2==="paper")
            {return"paper wins"}
            else if(choice2==="lizard")
            {return"stone 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"spock wins"}}
    else if(choice1 === "scissors"){
        if(choice2==="paper"){
            return"scissors wins"}
        else if(choice2==="stone"){
            return"stone wins"}
        else if(choice2==="lizard"){
            return"scissors wins"}
        else{
            return"spock wins"}}
    else if(choice1 === "lizard"){
        if(choice2==="paper"){
            return"lizard wins"}
        else if(choice2==="stone"){
            return"stone wins"}
        else if(choice2==="spock"){
            return"lizard wins"}
        else{
            return"scissors wins"}}
    else{ //             SPOCK
        if(choice2==="paper"){
            return"paper wins"}
        else if(choice2==="stone"){
            return"spock wins"}
        else if(choice2==="lizard"){
            return"lizard wins"}
        else{
            return"spock wins"}}
            }
if(userChoise!==rock){
    if(userChoice!==paper)}{
        if(userChoice!==scissors)}{
            if(userChoice!==lizard)}{
                if(userChoice!==spock)}{
                    console.log("You entered an invalid choice. You can choose from rock, paper, scissors, lizard and spock. Note that it doesn't work with capital letters.");}
compare(userChoice, computerChoice)

Someone please help me debug


#2

Else statements don't have conditions. it should just be "else {"


#3

SyntaxError: Unexpected token {


#4

if(userChoise!==rock){
if(userChoice!==paper)}{
if(userChoice!==scissors)}{
if(userChoice!==lizard)}{
if(userChoice!==spock)}{
console.log("You entered an invalid choice. You can choose from rock, paper, scissors, lizard and spock. Note that it doesn't work with capital letters.");}

Also, you have waaaaay to many opening brackets without closing brackets, change all of that to this:

if( userChoise!==rock && userChoice!==paper && userChoice!==scissors && userChoice!==lizard && userChoice!==spock){
 console.log("You entered an invalid choice. You can choose from rock, paper, scissors, lizard and spock. Note that it doesn't work with capital letters.");
}

You can also just use an empty "else" statement at the end of your closing else if statement so you don't have to worry about that mess that I just showed you.

It's best to indent your code so you can see where you have opening and closing brackets that need to be taken care of.


#5

Can you please explain indent further, i'm not a native englishman


#6

var compare = function(choice1, choice2) {
  if(choice1 === choice2) { //This line is indented
    return "The result is a tie!" //This line is even further indented
  }/*This is the closing bracket which has been indented to the same line as
   The opening bracket.*/

Basically, indenting something is to push it further down so you can see a difference in the lines and where things match up. This is super useful for things like what I just showed you.


#7

I fully indented it and it still didn't work :confused:


#8

indenting doesn't fix bugs it's just easier for you or others to find bugs if your code has some structure

PS: Also be careful with statements like this:

else if(choice2===scissors)

because without "" the scissors is treated as a variable.


#9

I know, i searched, but i found another way of debugging.
You put an invalid identifier under a section (like "va ducks"), and if it still says the same error, than you know the bug is in that section. If it says "invalid identifier" you know it's not in any of the code stated above "va ducks"


#10

The Code is now fully debugged! Here you go
var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?");
var computerChoice = Math.random();
if (computerChoice <= 0.19) {
computerChoice = "rock"}
else if(computerChoice <= 0.40) {
computerChoice = "paper"}
else if(computerChoice <= 0.60){
computerChoice = "scissors"}
else if(computerChoice <= 0.80){
computerChoice = "lizard"}
else computerChoice = "spock"
console.log("Computer: " + computerChoice);
//everything stated below should be fine
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!"
}
else if(choice1 === "rock") {
if(choice2==="scissors") {
return"rock wins"}
else if(choice2==="paper"){
return"paper wins"}
else if(choice2==="lizard"){
return"stone 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"spock wins"}
}
else if(choice1 === "scissors"){
if(choice2==="paper"){
return"scissors wins"}
else if(choice2==="stone"){
return"stone wins"}
else if(choice2==="lizard"){
return"scissors wins"}
else{
return"spock wins"}
}
else if(choice1 === "lizard"){
if(choice2==="paper"){
return"lizard wins"}
else if(choice2==="stone"){
return"stone wins"}
else if(choice2==="spock"){
return"lizard wins"}
else{
return"scissors wins"}
}
else{
if(choice2==="paper"){
return"paper wins"}
else if(choice2==="stone"){
return"spock wins"}
else if(choice2==="lizard"){
return"lizard wins"}
else{
if( userChoice!=="rock" && userChoice!=="paper" && userChoice!=="scissors" && userChoice!=="lizard" && userChoice!=="spock"){
console.log("You entered an invalid choice. You can choose from rock, paper, scissors, lizard and spock. Note that it doesn't work with capital letters.");
}
else{return"spock wins"}
}

}
}
compare(userChoice, computerChoice)

Have fun :stuck_out_tongue:


#11

Ok that is an interesting approach to lazy evaluation :smile: