SYST10199 Chapter 1: nested if for javascript
var count, userselect, computerselect;
function playFunction() {
//Ask the user to choose their move using a prompt() dialog box.
var userselect = prompt("Please type in R for Rock, P for Paper and S for
Scissors.", "R");
//Keep asking the user their move if the user enters some invalid input.
if (userselect!=="R" && userselect!=="P" && userselect!=="S") {
alert("Invalid Input, please try again. Type in R,P, or S.");
} <!--end if-->
else{
computerplay()}
}<!--end function loop-->
<!--continue with game-->
function computerplay(){
<!--computer chooses its move randomly-->;
var computerselect = Math.random();
if (computerselect <=0.33) {
computerselect = "R";
alert("The computer picks Rock");
} else if(computerselect >0.33 && computerChoice<=0.66) {
computerselect = "P";
alert("The computer picks Paper");
} else {
computerselect = "S";
alert("The computer picks Scissors");
}<!--end if-->
}<!--end the computerplay method-->
<!--run the comparethis function-->
function comparethis(){//start method
if (computerselect === userselect)
{
alert("It's a tie!");
}
else if (computerselect === "R")
{
if(userselect === "S")
{
alert("You lose!");
}
else (userselect === "P")
{
alert("You win!");
}
}
else if (computerselect === "P")
{
if (userselect === "R")
{
alert("You lose!");
}
else if (userselect === "S")
{
alert("You win!");
}
}
else (computerselect === "S")
{
if (userselect === "R")
{
alert("You win!");
}
else if (userselect === "P")
{
alert("You lose!");
}
} //run this while they select yes
do{again = prompt("Would you like to play again?(Yes/No)");
if (again === "No"); {
alert("The winner is" + winner);
}
}<!--end method-->
find more resources at oneclass.com
find more resources at oneclass.com