Build "Rock, Paper, Scissors"


#1

I need help looping at the end.

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!";
    }
    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";
        }
    }
    
};

var result = compare(userChoice, computerChoice);
if (result === "The result is a tie!") {
    prompt("Try again");
    for (prompt = "Try again".length - 9; prompt"Try again" < 2; prompt "Try again"++ ) {
        console.log(prompt("Try again"));
    }
}
else {
    console.log("Play again soon");
}


console.log(result);

#2

@cordell.h,

What you want is a loop-construct...
create the getUserChoice function using return-statement
create the getComputerChoice function using return-statement
creat the compare function using return-statement ( allready done !!)

As you are using return-statements you call the compare-function
var theResult = compare(userChoice,computerChoice);
and thus having the =result= of the game in the theResult variable

===========================================

var getUserChoice = function () {
 var userChoice;
 do {
      userChoice = prompt("Choose either rock, paper or scissors","paper");
     } while (userChoice !== "rock" &&
          userChoice !== "paper" &&
          userChoice !== "scissors");
     // console.log("UserChoice is 1: "+ userChoice);
     return userChoice;
};
var getComputerChoice = function() {
var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if(computerChoice <= 0.67) {
	computerChoice = "paper";
  } else {
	computerChoice = "scissors";
  }
  //console.log("computerChoice 1: " + computerChoice);
  return computerChoice;
};

//var userChoice = getUserChoice();
//var computerChoice = getComputerChoice();

//=================================

var compare = function(choice1, choice2){ ///opens the function
  if(choice1 === choice2){ ///opens the if
      return("The result is a tie!");
  } ///closes the if

  else if (choice1 === "rock"){ ///opens the else if
      if(choice2 === "scissors"){ ///opens the if
          return "rock wins";
      }  ///closes the if
      else{ ///opens the else
          return "paper wins";
      } ///closes the else
  } ///closes the else if

  else if (choice1 === "paper") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return "paper wins";
      } ///closes the if
      else { ///opens the else
          return "scissors wins";
      } ///closes the else
  } ///closes the else if

  else if (choice1 === "scissors") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return("rock wins");
      } ///closes the if
      else { ///opens the else
          return("scissors wins");
      } ///closes the else
  } ///closes the else if

}; ///closes the function

==============================================================

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";
    while (playAgain) {
    //execute the pre-declared getUserChoice-function
    userChoice = getUserChoice();
    console.log("User choice is " + userChoice);

```
        //execute the pre-declared getComputerChoice-function
        computerChoice = getComputerChoice();
        console.log("Computer choice is " + computerChoice);

        //execute the compare-function and capture the-result
        theResult = compare(userChoice,computerChoice);
        console.log("The result is " + theResult);

        //if (theResult === "It is a tie!") {
        //    console.log("It is a tie!");
        if (theResult === "The result is a tie!") {
            //console.log("The result is a tie!");
            playAgain = true;
            console.log("==== Play once more =====");
        } else {
            playAgain = false;
            console.log("==== End of Game =====");
        }

    //end of -while- loop is reached, and if VALUE of playAgain === true
    // the loop will start over
    } //closing the while-loop

}; //closing the function-body of playTheGame

##======================================================================

// call the function playTheGame
playTheGame();

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.