Rock,Paper,Scissors
A game created as part of the JavaScript course at Codeacademy.
by Brock Callahan
HTML
<h1>This exercise was initiated by Codeacademy.</h1>
<div id="output"></div>
<div id="output2"></div>
CSS
body {
background: #000;
color: #f8f8f8;
font-family: sans-serif;
}
JavaScript
var userChoice = prompt("Rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
writeIt("output", "Computer picked " + computerChoice + "...");
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "It was 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 === "rock") {
return "rock wins!";
} else {
return "scissors wins!";
}
} else {
return "You lose by forfeit!";
}
};
function writeIt(id, text) {
document.getElementById(id).innerHTML = text;
};
writeIt("output2", compare(userChoice, computerChoice));