<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>
<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
hey guys! I have no idea where i went wrong at! please help! Thanks!!!
/*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";
} console.log("Computer: " + computerChoice);*/
var compare = function (choice1, choice2){
if {choice1 === choice2} {
return"The result is a tie!";
}
}else if(choice1 === "paper")
{
if (choice2 === "rock") {return "paper wins";}
else {
return "rock wins";
}
}else if (choice1 === "scissors")
{
if(choice2 === "rock") {return "rock wins";}
else {return "scissors wins";}
}
}else if (choice1 === "rock"){
if(choice2 === "scissors") {return "rock wins";}
else {return "scissors wins";}
}
};
}
compare(userChoice, computerChoice);
^ this condition choice1 === choice2
its in curly brackets { }
its supposed to be in ( )
Hey zainabrawat, Thanks for the reply, I’ve changed that but it says “SyntaxError: Unexpected token else”, what should i do?
@thejoe98
Your code seems to be a little hard to read, can you edit it it a little bit?
like
if (choice2 === "rock") {return "paper wins";}
else {return "rock wins";}
instead of
if (choice2 === "rock") {return "paper wins";}
else{
return "rock wins";
}
and other stuff like that. That well make it a little easier to read.
As for your problem, it probably is an else statement that is causing it.
1 Like
Thanks for the reply printcoder_eric, I’ve changed the codes, however it stills says unexpected token else, I played around with the code but still, no luck, what I have found was in line 20 when i cancel out the else, the message comes up with unexpected token if… so I’m assuming that the problem is somewhere in line 20
thanks
Please paste in your code, its hard to debug from a picture
@zainabrawat Should I put down the code again?
Still struggling with the final part of this exercise. Would really love to finish this exercise. Here is my code so far:
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”;
} 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 {
return “paper wins”;
}
}
else if (choice1 === “paper”) {
if (choice2 === “rock”) {
return “paper wins”;
} else {
return “scissors wins”;
}
}
}
else if (choice1 === “scissors”) {
if (choice2 === “paper”) {
return “scissors wins”;
}
else {
return “rock wins”;
}
}
compare(userChoice,computerChoice);
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”;
} 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 {
return “paper wins”;
}
}
else if(choice1 === “paper”) {
if(choice2 === “rock”) {
return “paper wins”;
}
else {
return “scissors wins”;
}
}
else if(choice1 === “scissors”) {
if(choice2 ===“rock”) {
return “rock wins”;
}
else{
return “scissors wins”;
}
};
compare(userChoice, computerChoice);
What the heck is wrong with this?
Hi you should close the compare function at the end with a } after the closing } of the last else if statement
else if(choice1 === "scissors") {
if(choice2 ==="rock") {
return "rock wins";
}
else{
return "scissors wins";
}
};
}<== addd one here
1 Like
HI this part you should remove one } because it close the compare function before
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}
} <== remove that one
then add one at the end here
else if (choice1 === "scissors") {
if (choice2 === "paper") {
return "scissors wins";
}
else {
return "rock wins";
}
}
}<== add one here
for closing the compare function
@wizmarco I got it figured out now. Thank you very much for the help. I truly appreciate it. I’m starting to get better with Javascript.
2 Likes
I have similar problem, and I can not figure it out. Can someone see a mistake?
Here’s my code:
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”;
} 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{
return “paper wins”;
}}
else if(choice1 === “paper”){
if(choice2 === “rock”){
return “paper wins”;
}
else{
return “scissors wins”;
}
}
else if (choice1 === “scissors”){
if(choice2 === “rock”){
return “rock wins”;
}
else (choice2 === “paper”){
return === “scissors wins”;
}};
}
compare (userChoice,computerChoice);
HI in your last else if statement you put a condition to the else statement remove it. and inside it you have
return === "scissors wins";
remove the ===
Thank You. I removed this silly mistake, but it does not work at the end…
can you post your new code?
Yes, here it goes:
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”;
} 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{
return “paper wins”;
};}
else if(choice1 === “paper”){
if(choice2 === “rock”){
return “paper wins”;
}
else{
return “scissors wins”;
}
}
else if (choice1 === “scissors”){
if(choice2 ===“rock”){
return “rock wins”;
}
else (choice2=== “paper”){
return “scissors wins”;
}
};
}
compare (userChoice, computerChoice);
I said the else statement doesn’t take a condition
else (choice2=== "paper"){
return "scissors wins";
}
remove the (choice2=== “paper”)
1 Like