Reputation: 3
I've been stuck on 7/9 for 2 nights now. This is a rock, paper scissor game. I can't figure out what is wrong. I tried the online lint and it also says my line 22 is an error(Expected an identifier and instead saw 'else'). Following the instructions I wrote another else if under the existing code inside the compare function.
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";
}
}
Upvotes: 0
Views: 1272
Reputation: 4620
You cant have an else if
after an else
statement, thats what is causing your error message error(Expected an identifier and instead saw 'else')
And of course no semicolorn before the line really ends
https://jsfiddle.net/8hcpfnhw/
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 if(choice1 === "paper"){ // I changed this to else if instead of else
return "paper wins";
}
else if(choice1 ==="paper") {
if(choice2 ==="rock")
return("paper wins");
}
else{ // only else as last check
return"scissors wins";
}
}
Upvotes: 0
Reputation: 322
I'm seeing several syntax errors in your code. It should look like the following:
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";
}
}
}
Upvotes: 0
Reputation: 2823
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
you're terminating you're else if
after the condition with ;
it should be:
else if(choice1 ==="paper"){
if(choice2 ==="rock")
return("paper wins");
}
Upvotes: 1
Reputation: 506
I am assuming you are a beginner. Write a very clean code, take care of spaces and tabs, it is the best way to solve your debugging problems. there is indeed problem on line 22, you have put a semi-colon after a conditional statement.
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"){//here the error was.
if(choice2 === "rock")
return("paper wins");
} else {
return "scissors wins";
}
}
Upvotes: 1
Reputation: 1813
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");{ -- on this there is semicolon after elseif block.. and how come else if is there after else block..
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
Upvotes: 1