-
Notifications
You must be signed in to change notification settings - Fork 0
/
rockPaperScissors.js
57 lines (51 loc) · 1.17 KB
/
rockPaperScissors.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
'use strict';
// Codewars - Andreas Sosilo
// 8 kyu - Rock Paper Scissors!
/*
Task:
-----
Let's play! You have to return which player won! In case of a draw return Draw!.
Examples:
rps('scissors','paper') // Player 1 won!
rps('scissors','rock') // Player 2 won!
rps('paper','paper') // Draw!
*/
// SOLUTION
const rps = (p1, p2) => {
let result = '';
if (p1 === p2) {
result = `Draw!`;
} else if (p1 === 'scissors') {
if (p2 === 'paper') {
result = `Player 1 won!`;
} else {
result = `Player 2 won!`;
}
} else if (p1 === 'paper') {
if (p2 === 'rock') {
result = `Player 1 won!`;
} else {
result = `Player 2 won!`;
}
} else if (p1 === 'rock') {
if (p2 === 'scissors') {
result = `Player 1 won!`;
} else {
result = `Player 2 won!`;
}
}
return result;
};
// TEST CASES
console.log(rps('rock', 'scissors')); // "Player 1 won!"
// BEST 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!";
// }
// };