I’m writing a javascript rock paper scissors application, but I’ve never written an application before. Can anyone help me out? For Rock, it always outputs a draw,
for Paper, it always outputs a loss,
for Scissors, it always outputs a win.
<script type="text/javascript">
var Rock;
var Paper;
var Scissors;
var choice = prompt("Specify your choice of 'Rock', 'Paper', or 'Scissors'.");
var game = [Rock, Paper, Scissors];
document.write("Rock.");
document.write("Paper..");
document.write("Scissors...");
var result = game[Math.floor(Math.random() * game.length)];
if (result === Rock) {
if (choice === "Rock") {
document.write("It's a draw! Try again.");
}
else if (choice === "Paper") {
document.write("You lose! Unlucky.");
}
else if (choice === "Scissors") {
document.write("You win! Nice.");
}}
else if (result === Paper) {
if (choice === "Rock") {
document.write("You win! Nice.");
}
else if (choice === "Paper") {
document.write("It's a draw! Try again.");
}
else if (choice === "Scissors") {
document.write("You lose! Unlucky.");
}}
else if (result === Scissors) {
if (choice === "Rock") {
document.write("You lose! Unlucky.");
}
else if (choice === "Paper") {
document.write("You win! Nice.");
}
else if (choice === "Scissors") {
document.write("It's a draw! Try again.");
}}
</script>
Rock,PaperandScissorsare allundefined, so they’ll always be equal. Set them to anything and it should work.