Rock Paper Scissors Game - Code works, but not operating as I expect


#1

I changed some of the output language around to make sure I understand the if / else if / else statements, but the output is not what I expect. Can someone help explain why?

WHEN I RUN THIS:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerMathChoice = Math.random();
if (computerMathChoice < 0.34) {
	var computerChoice = "rock";
} else if (computerMathChoice <= 0.67) {
	var computerChoice = "paper";
} else {
	var computerChoice = "scissors";
} 
console.log("User: " + userChoice);
console.log("Computer Random Number: " + computerMathChoice)
console.log("Computer: " + computerChoice);
var compare = function (computerChoice, userChoice) 
{
    if (computerChoice === userChoice) 
    {
    return "The result is a tie!";
    }
    
    else if (computerChoice === "rock") 
    {
        if (userChoice === "scissors") 
        {
            return "Computer's rock beats user's scissors";
        }
        else 
        {
        return "User's paper beats computer's rock";
        }
    }
    
    else if (computerChoice === "paper") 
    {
        if (userChoice === "rock") 
        {
            return "Computer's paper beats user's rock";
        }
        else 
        {
            return "User's scissors beats computer's paper";
        }
    }
    
    else if (computerChoice === "scissors") 
    {
        if (userChoice === "paper") 
        {
            return "Computer's scissors beats user's paper";
        }
        else
        {
            return "User's rock beats computer's scissors";
        }
    }
};
console.log(compare(userChoice,computerChoice))

THE CONSOLE PRINTS THIS:

User: scissors
Computer Random Number 0.6566801814200625
Computer: paper
Computer's scissors beats user's paper

BUT SHOULDN'T IT PRINT THIS:

User: scissors
Computer Random Number 0.6566801814200625
Computer: paper
User's scissors beats computer's paper

I think I am misunderstanding how the if / else statements work. As I understand, the code should be using this code block based on the inputs, but it doesn't seem to be. Any help is much appreciated

    else if (computerChoice === "paper") 
    {
        if (userChoice === "rock") 
        {
            return "Computer's paper beats user's rock";
        }
        else 
        {
            return "User's scissors beats computer's paper";
        }
    }

#2

@websurfer95994,
I have tried your compare function with

console.log( compare("rock","rock"));
console.log( compare("paper","paper"));
console.log( compare("scissors","scissors"));
console.log("====================================");
console.log( compare("paper","rock"));
console.log( compare("paper","scissors"));
console.log("====================================");
console.log( compare("rock","paper"));
console.log( compare("rock","scissors"));
console.log("====================================");
console.log( compare("scissors","paper"));
console.log( compare("scissors","rock"));

and got the output

The result is a tie!
The result is a tie!
The result is a tie!
====================================
Computer's paper beats user's rock
User's scissors beats computer's paper
====================================
User's paper beats computer's rock
Computer's rock beats user's scissors
====================================
Computer's scissors beats user's paper
User's rock beats computer's scissors

#3

hmm @leonhard.wettengmx.n

is there something wrong with code academy then? See below screenshot:


#4

@websurfer95994,

In some sections of this course, the course-checker Display's data
this you could then identify by placing two EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

I suspect the code-checker-sofware
uses the
call compare( userChoice, computerChoice)
instead of your compare( computerChoice, userChoice )

Thus...
add the 2 code-lines

console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

to the end of code....