Reputation: 169
I made a rock paper scissors game, but the window.alert is not working, so users can't see whether they won or not! How would I fix this? Here is my JS:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!";
window.alert("Wait what. We tied?");
}
else if(choice1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
window.alert("What? Why u crush my scissors :<. I guess you won!");
}
else {
return "paper wins";
window.alert("Got a present for you! Just kidding lol its a rock packed in paper. Imma throw it at you and you will die.");
}
}
if(choice1 === "paper")
if(choice2 ==="rock"){
return "paper wins";
window.alert("Your paper, Vs my Rock! Hah! I won!");
}
if(choice1 === "scissors"){
if(choice2 ==="rock"){
return "rock wins";
window.alert("I just crushed your scissors fam. I won. ezpz take the L");
}
else{
return "scissors wins";
window.alert("You spooked m8? My scissors cut you in 326 pieces! >:D");
}
}
};
compare(userChoice,computerChoice);
Everything else works fine, but it just prints to console. Help is appreciated
Upvotes: 1
Views: 293
Reputation: 376
The alerts are not reachable as you have a return statement before they are called, which exits the function. And you had a missing curly brace and it required an additional else, this was easy to spot once I indented the code correctly.
Try:
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!";
window.alert("Wait what. We tied?");
} else {
if(choice1 === "rock") {
if(choice2 === "scissors") {
alert("What? Why u crush my scissors :<. I guess you won!");
return "rock wins";
} else {
alert("Got a present for you! Just kidding lol its a rock packed in paper. Imma throw it at you and you will die.");
return "paper wins";
}
}
if(choice1 === "paper") {
if(choice2 ==="rock") {
alert("Your paper, Vs my Rock! Hah! I won!");
return "paper wins";
}
}
if (choice1 === "scissors") {
if(choice2 ==="rock"){
alert("I just crushed your scissors fam. I won. ezpz take the L");
return "rock wins";
} else {
return "scissors wins";
}
}
};
compare(userChoice,computerChoice);
Upvotes: 0
Reputation: 477
you are missing some
else
and some curly braces
and please fix your indentation ._.
Upvotes: 0
Reputation: 98
Except for one, the returns come before the alert. A return breaks you out of a function, the rest of the lines don't run, try switching them all around
Upvotes: 1
Reputation: 577
The window.alert() calls are unreachable by the program. When the program encounters a return statement, execution returns back to where the function was called from. You have to move the window.alert() calls above each return statement, so that they are called if you want the alerts to be shown.
Also, using "window." in each alert statement is optional.
Upvotes: 4