-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps3.html
102 lines (80 loc) · 3.14 KB
/
rps3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// starting from scratch on rps project
<script>
// setting the function calls to global variables as it seems to be the only way to get this done...
let playerC = playerChoice();
let compC = compChoice();
// functions
// compChoice uses built in JS math functions to randomly pick from the provided array and return value
function compChoice(){
let handChoice = ["Rock", "Paper", "Scissors"] // array of possible choices
let rando = handChoice[Math.floor(Math.random()*handChoice.length)]; //
return rando.toString();
}
// playerChoice prompts for player choice and formats input to capitalize first char and lowercase the rest and returns value
function playerChoice(){
let playerChoice = prompt("What are you throwing down? Rock, Paper, or Scissors? Type your answer in.");
let playerToLower = playerChoice.toLowerCase();
let firstChar = playerToLower.charAt(0);
let firstCharCap = firstChar.toUpperCase();
let fPlayerChoice = firstCharCap + playerToLower.slice(1);
return fPlayerChoice.toString();
}
// tie evaluates if there is a tie, returns value of 'tied' or 'not tied'
function tie() {
if ( playerC === compC ) {
let game = 'tied'
return game;
} else {
//alert("Not a tie, let's see who won!")
console.log("Not a tie. Let's see who won.");
let game = 'not tied'
return game;
}
return game;
}
// evalWin evaluates playerC and compC to determine a winner. Defaults to invalid input if playerC is not one of the accepted values.
function evalWin() {
if (tie() === 'tied') {
//alert(`You both threw down '${compC}' it's a Tie.`);
console.log(`You both threw down '${compC}' it's a Tie.`)
} else {
// rock
if (playerC === 'Rock') {
if (compC === 'Scissors') {
//alert("Rock Crushes Scissors, You Win!");
console.log("Rock Crushes Scissor. Player Wins");
} else {
//alert("Paper Covers Rock, You Lose!");
console.log("Paper Covers Rock - Player Loses.");
}
// scissors
} else if (playerC === 'Scissors') {
if (compC === 'Paper') {
//alert("Scissors Shreds Paper, You Win!");
console.log("Scissors Shreds Paper - Player Wins.");
} else {
//alert("Rock Crushes Scissors, You Lose!");
console.log("Player Loses.");
}
// paper
} else if (playerC === 'Paper') {
if (compC === 'Rock') {
//alert("Paper Covers Rock, You Win!");
console.log("Paper Covers Rock - Player Wins.");
} else {
//alert("Scissors Shreds Paper, You Lose!");
console.log("Scissors Shreds Paper - Player Loses.");
}
// anything else
} else {
//alert("Round Forfeit. Make sure you spell your word correctly next time.");
console.log("Player Loses. Player did not enter a valid word.");
}
}
}
function playRound(playerC, compC){
tie();
evalWin();
}
playRound();
</script>