SHOW:
|
|
- or go back to the newest paste.
1 | var userChoice = ""; | |
2 | - | var incorrectAnswer = true; |
2 | + | |
3 | var translate = 0; | |
4 | var noWinner = true; | |
5 | - | var tie = false; |
5 | + | |
6 | ||
7 | function verifyChoice(a){ | |
8 | - | var verifyChoice = function(choiceAnswer){ |
8 | + | if((a === "rock") || (a === "paper") || (a === "scissors")){ |
9 | - | if((choiceAnswer === "rock") || (choiceAnswer === "paper") || (choiceAnswer === "scissors")){ |
9 | + | return 0; |
10 | - | incorrectAnswer = false; |
10 | + | |
11 | else { | |
12 | return 1; | |
13 | - | confirm("That is not a valid option"); |
13 | + | |
14 | - | incorrectAnswer = true; |
14 | + | |
15 | ||
16 | ||
17 | function computerRandom(){ | |
18 | translate = Math.random(); | |
19 | - | var computerRandom = function(){ |
19 | + | |
20 | return "rock"; | |
21 | } | |
22 | else if(translate <= 0.67){ | |
23 | return "paper"; | |
24 | } | |
25 | else{ | |
26 | return "scissors"; | |
27 | } | |
28 | }; | |
29 | ||
30 | //returns true if choice wins | |
31 | function compare(choice, computerChoice){ | |
32 | if ((choice === "rock" && computerChoice === "scissors")|| | |
33 | - | var compare = function(choice1){ |
33 | + | (choice === "paper" && computerChoice === "rock")|| |
34 | - | switch(choice1){ |
34 | + | (choice === "scissors" && computerChoice === "paper")){ |
35 | - | case "rock": |
35 | + | return 1; |
36 | - | switch(computerChoice){ |
36 | + | } else { |
37 | - | case "scissors": |
37 | + | return 0; |
38 | - | return "You win!"; |
38 | + | } |
39 | - | break; |
39 | + | |
40 | - | case "paper": |
40 | + | |
41 | - | return "Computer wins, you suck!"; |
41 | + | |
42 | - | break; |
42 | + | |
43 | userChoice = prompt("Do you choose rock, paper or scissors?"); | |
44 | - | case "paper": |
44 | + | if(verifyChoice(userChoice)){ |
45 | - | switch(computerChoice){ |
45 | + | computerChoice = computerRandom(); |
46 | - | case "rock": |
46 | + | confirm("Computer chose: " + computerChoice); |
47 | - | return "You win!"; |
47 | + | if(computerChoice === userChoice){ |
48 | - | break; |
48 | + | confirm("It's a tie! Try again!"); |
49 | - | case "scissors": |
49 | + | }else if(compare(userChoice,computerChoice)){ |
50 | - | return "Computer wins, you suck!"; |
50 | + | confirm("You win!"); |
51 | - | break; |
51 | + | noWinner = false; |
52 | }else{ | |
53 | - | case "scissors": |
53 | + | confirm("Computer wins, you suck!"); |
54 | - | switch(computerChoice){ |
54 | + | noWinner = false; |
55 | - | case "paper": |
55 | + | } |
56 | - | return "You win!"; |
56 | + | }else{ |
57 | - | break; |
57 | + | confirm("That is not a valid option"); |
58 | - | case "rock": |
58 | + | } |
59 | - | return "Computer wins, you suck!"; |
59 | + | }while(noWinner); |