/
index.js
74 lines (68 loc) · 1.72 KB
/
index.js
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
const options = ['rock','paper','scissors'];
const playerPlay = () => {
let selection="";
while(!options.includes(selection.toLowerCase())){
selection = prompt("Rock, Paper or Scissors", "rock")
}
return selection;
}
const computerPlay = () => {
return options[Math.floor(Math.random()*options.length)]
}
const whoWon = (player, computer) =>{
// the ties, we handle right off.
if(player===computer) return 0;
switch(player){
case "rock":
if(computer=="paper") return -1;
return 1;
case "paper":
if(computer=="scissors") return -1;
return 1;
case "scissors":
if(computer=="rock") return -1;
return 1;
}
}
const playRound = () => {
const playerSelection = playerPlay();
const computerSelection = computerPlay();
const wonThisRound = whoWon(playerSelection, computerSelection );
switch(wonThisRound){
case 0:
return {
message: `It's a tie: ${playerSelection} and ${computerSelection}`,
player: 0,
computer: 0
};
case 1:
return {
message: `You won! ${playerSelection} beats ${computerSelection}`,
player: 1,
computer: 0
};
default:
return {
message: `You lose. ${computerSelection} beats ${playerSelection}`,
player: 0,
computer: 1
};
}
}
const game = () => {
let scores = {
player: 0,
computer: 0
};
let result;
for(let roundCount=0; roundCount<=5; roundCount++){
result = playRound();
console.log(JSON.stringify(result))
scores.player += result.player;
scores.computer += result.computer;
console.log(result.message);
console.log(`Player: ${scores.player}, Computer: ${scores.computer}`)
}
result=undefined;
}
game();