This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by morfh
about 11 years

Can someone please debug/find an error for my code?

When I run my code, I always get this error message: Oops, try again. It looks like your compare function doesn’t return ‘rock wins’ when comparing scissors to rock.

I’m not sure what is wrong with my code, been sifting through it for 30 minutes now.

var userChoice = prompt(“Do you choose rock, paper or scissors?”); var computerChoice = Math.random(); if (computerChoice <0.34){ computerChoice = “rock”; }else if(computerChoice <=0.67){ computerChoice = “paper”; }else{ computerChoice = “scissors”; } var compare = function(choice1, choice2){ if (choice1===choice2) return “The result is a tie!”; if (choice1===”rock”){ if (choice2===”scissors”){ return “rock wins”; } else { return “paper wins”; } } if (choice1===”paper”){ if (choice2===”rock”) return “paper wins”; } else { return “scissors wins”; } if (choice1===”scissors”){ if (choice2===”paper”) return “scissors wins”; } else { return “rock wins”; } }; compare(userChoice, computerChoice);

Again, I apologize if the brackets are out of place or seem confusing but it doesn’t seem to cause an error to the code. I’m new to coding so please help me out, thanks.

Answer 513a1005c9690f8be20008b3

0 votes

Permalink

Hi, you have a broblem with brackets. It should look like this:

if (condition) {
     if (condition) {
         return "some string";
     } else {
         return "some other string";
}

You’re missing brackets in second and third block And don’t forget about general brackets, I mean: var compare = function(choice1, choice2){ first block second block third block }; ps/ sorry if my english is no good =)

points
Submitted by killarmy
about 11 years

4 comments

morfh about 11 years

Can you copy and paste which part(s)? I still don’t see an error in my brackets and if I add or take away and brackets, it shows an error on the left side-bar.

Victor about 11 years

I see it now. Just check that after EVERY if (condition) You follow it up with a “{“ and then make sure you close the bracket with “}”

killarmy about 11 years

yep Victor, this brackets also drove me crazy at first too

morfh about 11 years

I see it too, thanks guys.

Answer 513a187e39bd92efc4000ee0

0 votes

Permalink

You write this:

    if (choice1==="paper"){
     if (choice2==="rock")------ ---//miss
     return "paper wins";
     } else {
     return "scissors wins";
     }
    
     if (choice1==="scissors"){
     if (choice2==="paper") ---- -----//miss
     return "scissors wins";
     }
     else {
     return "rock wins";
     }
     };

right be this:
if (choice1 === "paper"){
    if (choice2 === "rock"){
        return "paper wins";
    } else {
        return "scissors wins";
    }
}
if (choice1 === "scissors"){
    if (choice2 === "paper"){
        return "scissors wins";
    } else {
        return "rock wins";
    }
}
};
points
Submitted by killarmy
about 11 years

1 comments

morfh about 11 years

ah I see it now! Thanks a bunch! Never knew brackets were such a pain lol