SYST10199 Chapter 1: nested if for javascript

104 views1 pages
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
Unlock document

This preview shows half of the first page of the document.
Unlock all 1 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents