This forum is now read-only. Please use our new forums! Go to forums
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
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 =)
Answer 513a187e39bd92efc4000ee0
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";
}
}
};
1 comments
ah I see it now! Thanks a bunch! Never knew brackets were such a pain lol
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
4 comments
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.
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 “}”
yep Victor, this brackets also drove me crazy at first too
I see it too, thanks guys.