-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7kyuRockPaperScissorsLizardSpock.js
66 lines (58 loc) · 1.86 KB
/
7kyuRockPaperScissorsLizardSpock.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
//In this kata, your task is to implement an extended version of the famous rock-paper-scissors game. The rules are as follows:
// Scissors cuts Paper
// Paper covers Rock
// Rock crushes Lizard
// Lizard poisons Spock
// Spock smashes Scissors
// Scissors decapitates Lizard
// Lizard eats Paper
// Paper disproves Spock
// Spock vaporizes Rock
// Rock crushes Scissors
// Task:
// Given two values from the above game, return the Player result as "Player 1 Won!", "Player 2 Won!", or "Draw!".
function rpsls(pl1,pl2){
if (pl1 === 'scissors' && pl2 === 'paper') {
return 'Player 1 Won!';
} else if (pl1 === 'paper' && pl2 === 'rock') {
return 'Player 1 Won!';
} else if (pl1 === 'rock' && pl2 === 'lizard') {
return 'Player 1 Won!';
} else if (pl1 === 'lizard' && pl2 === 'spock') {
return 'Player 1 Won!';
} else if (pl1 === 'spock' && pl2 === 'scissors') {
return 'Player 1 Won!';
} else if (pl1 === 'scissors' && pl2 === 'lizard') {
return 'Player 1 Won!';
} else if (pl1 === 'lizard' && pl2 === 'paper'){
return 'Player 1 Won!';
} else if (pl1 === 'paper' && pl2 === 'spock'){
return 'Player 1 Won!';
} else if (pl1 === 'spock' && pl2 === 'rock'){
return 'Player 1 Won!';
} else if (pl1 === 'rock' && pl2 === 'scissors'){
return 'Player 1 Won!';
} else if (pl1 === pl2) {
return 'Draw!';
} else {
return 'Player 2 Won!';
}
}
//solution2
function rpsls(pl1, pl2) {
if (pl1 === pl2) {
return 'Draw!';
}
let rules = {
'paper': ['rock', 'spock'],
'rock': ['lizard', 'scissors'],
'lizard': ['spock', 'paper'],
'spock': ['scissors', 'rock'],
'scissors': ['lizard', 'paper']
}
if (rules[pl1].includes(pl2)) {
return 'Player 1 Won!';
} else {
return 'Player 2 Won!';
}
}