Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var getRandomInt = function (min, max)
- {
- return Math.floor(Math.random() * (max - min)) + min;
- }
- var compareChoice = function (choice1, choice2)
- {
- if (compareValueChoice("Paper", "Rock", choice1, choice2))
- {
- return ("Paper wins");
- }
- else if (compareValueChoice("paper", "Scissors", choice1, choice2))
- {
- return ("Scissors wins");
- }
- else if (compareValueChoice("Scissors", "Rock", choice1, choice2))
- {
- return ("Rock wins");
- }
- return ("Tie");
- }
- var compareValueChoice = function (value1, value2, choice1, choice2)
- {
- if (value1 === choice1 && value2 === choice2)
- {
- return true;
- }
- else if (value1 === choice2 && value2 === choice1)
- {
- return true;
- }
- return false;
- }
- var playGame = function ()
- {
- var userChoice = ""
- while (userChoice != "Rock" || userChoice != "Paper" || userChoice != "Scissors")
- {
- userChoice = prompt("Do you choose Rock, Paper or Scissors?");
- }
- console.log("You chose " + userChoice);
- var aiChoice = ""
- switch(getRandomInt(1, 4))
- {
- Case 1:
- aiChoice = "Rock";
- break;
- Case 2:
- aiChoice = "Paper";
- break;
- case 3:
- aiChoice = "Scissors";
- break;
- }
- console.log("Computer chose " + aiChoice);
- return compareChoice(userChoice, aiChoice)
- }
- var result = ""
- do
- {
- result = playGame()
- } while (result == "Tie")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement