Oops, try again. There was a problem with your syntax.(what is the problem?)


#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"
}
}


#2

you forgot to close the function.


#3

My code works but does not announce winner!!!

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(choise1==="scissors"){
if(choise2==="rock"){
return"rock wins"
}
else {
return"scissors wins"
}
}
}


#4

because you are returning the value so it's working but it's not shown.
try console.log


#5

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!"
console.log( "The result is a tie!" )
}

is it right??


#6

Warning: unreachable code after return statement
for more details see here

if (choice1 === choice2) {
return "The result is a tie!"  // return exits the function immediately
console.log( "The result is a tie!" )// so this line will never run; it is unreachable

you dont need to do that.your first code was right.

The code in a function is not executed when the function is defined. It is executed when the function is invoked.Some people use the term "call a function" instead of "invoke a function".
so simply you have to call the function by function name with arguments.for example

function hello(thing) {
console.log("Hello " + thing);
}
hello("world")//calling a function

here thing is a parameter and world is a argument(real value)


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.