Can't get the return outputs


#1

When I input something besides (rock, paper, or scissor) the output isn't my return prompt. I also can't get the returns to come up like "rock wins" etc..


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

compare = function(userChoice,computerChoice) 
{
    if (userChoice === computerChoice) 
    {return "The result is a tie!";}
    
    else if (userChoice ==="rock")
    
    {if (computerChoice ==="scissors") 
        {return "rock wins";}
            else 
            {return "paper wins";}
            
    if (userChoice ==="paper") 
    {
        if (computerChoice ==="rock") 
        {return "paper wins";}
            else 
            {return "rock wins";}
            
    if (userChoice ==="scissors") 
    {
        if (computerChoice ==="rock") 
        {return "rock wins";}
            else 
            {return "scissors wins";}
    
    if (userChoice !== "rock","paper","scissor")
        {return prompt ("Please choose rock, paper, or  scissor");}
    }
    }
    }
};


#2

The if statement should have the following form:

   if (condition) {
        statements
    }
    
// or
    if (condition) {
        statements
    } else {
        statements
    }

// or    
    if (condition) {
        statements
    } else if (condition) {
        statements
    } else {
        statements
    }

// nested
   if (condition) {
        if (condition) {
           statements
        }
    } else if (condition) {
        if (condition) {
           statements
        } else {
           statements
        }
    } else {
        if (condition) {
           statements
        } else if (condition) {
           statements
        } else {
           statements
        }
    }

Please, format your code. I'm sure it's help you. Did you call function compare() ?

Edit. Clean code avoid syntax errors and makes your code readable.


#3

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