Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var userStartFunc = function(){
- var userChoice = prompt("Do you choose rock, paper or scissors?");
- return userChoice
- }
- var computerStartFunc = function(){
- var computerChoice = Math.random();
- if (computerChoice < 0.34) {
- computerChoice = "rock";
- }
- else if(computerChoice <= 0.67) {
- computerChoice = "paper";
- }
- else {
- computerChoice = "scissors";
- }
- return computerChoice;
- }
- var compare = function(choice1, choice2){
- 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 endFunc = function(userChoice, computerChoice){
- if(userChoice == "rock" || userChoice == "scissors" || userChoice == "paper"){
- var endResult = compare(userChoice, computerChoice);
- document.write("Computer choice: "+computerChoice+"<br>");
- document.write("Your choice: "+userChoice+"<br>");
- document.write(endResult+"<br>");
- }
- else{
- document.write("Bad choice! This is impossible!");
- }
- }
- var i = 0;
- var status = 1;
- do{
- userChoice = userStartFunc();
- computerChoice = computerStartFunc();
- if(userChoice == computerChoice && status != 1){
- document.write("The result is a tie!");
- i = 0;
- }
- else{
- endFunc(userChoice, computerChoice);
- i = 1;
- }
- status = 2;
- }while(i == 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement