-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattle.js
53 lines (51 loc) · 1.01 KB
/
battle.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
// Description:
// Groups of characters decided to make a battle. Help them to figure out which group is more powerful.
// Create a function that will accept 2 strings and return the one who's stronger.
// Rules:
// Each character have its own power: A = 1, B = 2, ... Y = 25, Z = 26
// Strings will consist of uppercase letters only
// Only two groups to a fight.
// Group whose total power (A + B + C + ...) is bigger wins.
// If the powers are equal, it's a tie.
function battle(x, y) {
const al = [
" ",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
];
const totalX = x
.split("")
.map((i) => al.indexOf(i))
.reduce((a, b) => a + b, 0);
const totalY = y
.split("")
.map((i) => al.indexOf(i))
.reduce((a, b) => a + b, 0);
if (totalX > totalY) return x;
if (totalX < totalY) return y;
return "Tie!";
}