mardi 12 juillet 2016

Javascript Rock Paper Scissors

why is my programme not working?

is there something wrong with my Math.random?

"Do you choose Rock, Paper or Scissors?"

"Do you choose Rock, Paper or Scissors?"

"Do you choose Rock, Paper or Scissors?"

//////////////////////////////

var userChoice = prompt("Do you choose Rock, Paper or Scissors?")

var computerChoice = Math.random();

//======================================

if(computerChoice <= 0.33 )
{
    computerChoice = "Rock";
}

else if(computerChoice <= 0.66)
{
    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();

Aucun commentaire:

Enregistrer un commentaire