-
Notifications
You must be signed in to change notification settings - Fork 25
/
(8 kyu) Rock Paper Scissors.js
38 lines (36 loc) · 1.15 KB
/
(8 kyu) Rock Paper Scissors.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
// 1 Plain solution
const rps = (p1, p2) => {
if (p1 === 'scissors' && p2 === 'paper') {
return 'Player 1 won!';
} else if (p1 === 'scissors' && p2 === 'rock') {
return 'Player 2 won!';
} else if (p1 === 'rock' && p2 === 'paper') {
return 'Player 2 won!';
} else if (p1 === 'rock' && p2 === 'scissors') {
return 'Player 1 won!';
} else if (p1 === 'paper' && p2 === 'rock') {
return 'Player 1 won!';
} else if (p1 === 'paper' && p2 === 'scissors') {
return 'Player 2 won!';
} else {
return 'Draw!';
}
};
// 2 Optimized solution
const rps = (p1, p2) => {
if (p1 === p2) return 'Draw!';
if (p1 === 'rock' && p2 === 'scissors') return 'Player 1 won!';
if (p1 === 'scissors' && p2 === 'paper') return 'Player 1 won!';
if (p1 === 'paper' && p2 === 'rock') return 'Player 1 won!';
return 'Player 2 won!';
};
// 3 Clever solution
const rps = (p1, p2) => {
if (p1 === p2) return "Draw!";
var rules = { rock: "scissors", paper: "rock", scissors: "paper" };
if (p2 === rules[p1]) {
return "Player 1 won!";
} else {
return "Player 2 won!";
}
};