-
Notifications
You must be signed in to change notification settings - Fork 1
/
Alphabet war.js
40 lines (33 loc) · 1.45 KB
/
Alphabet war.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
// Write a function that accepts fight string consists of only small letters and return who wins the fight. When the left side wins return Left side wins!, when the right side wins return Right side wins!, in other case return Let's fight again!.
// The left side letters and their power:
// w - 4
// p - 3
// b - 2
// s - 1
// The right side letters and their power:
// m - 4
// q - 3
// d - 2
// z - 1
// The other letters don't have power and are only victims.
// Example
// alphabetWar("z"); //=> Right side wins!
// alphabetWar("zdqmwpbs"); //=> Let's fight again!
// alphabetWar("zzzzs"); //=> Right side wins!
// alphabetWar("wwwwwwz"); //=> Left side wins!
//P: string
//R: When the left side wins return Left side wins!, when the right side wins return Right side wins!, in other case return Let's fight again!.
//E:
//P:
function alphabetWar(fight)
{
const dictL={w:4,p:3,b:2,s:1},dictR={m:4,q:3,d:2,z:1};
const arr = fight.split('');
const left=arr.filter(v=>v.match(/[wpbs]/g)).map(v=>dictL[v]).reduce((a,b)=>a+b,0);
const right=arr.filter(v=>v.match(/[mqdz]/g)).map(v=>dictR[v]).reduce((a,b)=>a+b,0);
return left>right?"Left side wins!":left<right?"Right side wins!":"Let's fight again!";
}
console.log( alphabetWar("zdqmwpbs") , "Let's fight again!" )
console.log( alphabetWar("zzzzs"), "Right side wins!" )
console.log( alphabetWar("wwwwww"), "Left side wins!" )
// alphabetWar("zdqmwpbs")