Sunday, January 11, 2015

Paper Rock Scissors Rope javascript game

This is how to do a "paper rock scissors rope" game in javascript.  Just right-click the browser, then open "Inspect element" and go to Console tab to see the choice of the computer and the winner.

$(document).ready(function () {
var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var computerChoice = Math.random();
if (computerChoice < 0.25)
{
computerChoice = "rock";
}
else if(computerChoice <= 0.5)
{
computerChoice = "paper";
}
else if(computerChoice <= 0.75)
{
computerChoice = "scissors";
}
else if(computerChoice <= 1)
{
    computerChoice = "rope";
}
console.log(computerChoice);

var compare = function (choice1, choice2) {
    if (choice1 === choice2) {
        console.log("The result is a tie!");
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            console.log("rock wins");
        }
        else if (choice2 === "paper") {
            console.log("paper wins");
        }
        else {
            console.log("rope wins");
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            console.log("paper wins");
        }
        else if (choice2 === "scissors") {
            console.log("scissors wins");
        }
        else {
            console.log("rope wins");
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            console.log("rock wins");
        }
        else if (choice2 === "paper") {
            console.log("scissors wins");
        }
        else {
            console.log("rope wins");
        }
    }
    else if (choice1 === "rope") {
        if (choice2 === "rock") {
            console.log("rope wins");
        }
        else if (choice2 === "paper") {
            console.log("rope wins");
        }
        else {
            console.log("rope wins");
        }
    }
}
compare(userChoice, computerChoice);
});

No comments: