Why this code doesn't return the result


#1


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 (choice1 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
   }
 }
}

It only display Computer choice but Codeacademy says it's correct


#2

you only created the function, for a function to execute you need to call it


#3

ouch!

compare(userChoice, computerChoice);

but there is something wrong in the code, if I choose scissors it doesn't work


#4

you still need to add a else if condition for scissors in the next exercise


#5

Ouch again! too much code for today....

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);

#6

you have a problem?


#7

No! I think it is correct now, right?


#8

yes, it is :slight_smile: well done :slight_smile:


#9

Thanks stetim94, this forum is very helpful :+1:


#10