mercredi 30 décembre 2015

Rock, Paper, Scissors game, Codecademy Javascript

I built this as part of completing the Codecademy Javascript lesson. I wanted to perfect it, make it readable and functional, and as efficient as possible. As far as I can tell it can't be any more efficient than this. But maybe I can't tell. What do you think? I know there are little things, maybe the victory variable should have a console.log in it. Probably one or two more. But hopefully this is a good example for people who need help with writing their Rock, Paper, Scissors challenge.

Cheers,

Rich

//Loop ensures only valid user input is rock, paper, scissors
var i, infiniteloop;
for (i=0; i<1; infiniteloop++) //infiniteloop allows loop to continue if user is infinitely retarded
{
    var userChoice = prompt("You're playing rock, paper, scissors against the computer. Type your choice in lower case letters.")
    if (userChoice === "rock") {i=1;} //escapes loop
        else if (userChoice === "paper") {i=1;} //escapes loop
        else if (userChoice === "scissors") {i=1;} //escapes loop
        else {alert("Try again, stupid.");} //alerts, loops back to start
}

console.log("You chose " + userChoice + "!"); //announces userChoice

//generate a random computer choice, assign a string to replace it
var computerChoice = Math.random(); //random function. generates a number between 0 and 1
if (computerChoice <= .33) { computerChoice = "rock"; }
    else if (computerChoice <= .66) { computerChoice = "paper"; }
    else { computerChoice = "scissors"; }

console.log("Computer chose " + computerChoice + "!"); //announces computerChoice

var victory = "You win! A master strategist is you." //preset victory announcement
if (userChoice === computerChoice) { console.log("Tie game!"); }
    else if (userChoice === "rock" && computerChoice === "scissors") { victory }
    else if (userChoice === "paper" && computerChoice === "rock") { victory }
    else if (userChoice === "scissors" && computerChoice === "paper") { victory }
    else {console.log("You have suffered a devastating loss.");}

Aucun commentaire:

Enregistrer un commentaire