-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabet-war.js
More file actions
76 lines (63 loc) · 2.19 KB
/
Copy pathalphabet-war.js
File metadata and controls
76 lines (63 loc) · 2.19 KB
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
67
68
69
70
71
72
73
74
75
76
//7 Kyu
//Alphabet War
//Fundamentals, strings
// Introduction
// There is a war and nobody knows - the alphabet war!
// There are two groups of hostile letters. The tension between left side letters and right side letters was too high and the war began.
// Task
// 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. Sum up each side's letters' power values to determine which side wins.
// Example
// alphabetWar("z"); //=> Right side wins!
// alphabetWar("zdqmwpbs"); //=> Let's fight again!
// alphabetWar("zzzzs"); //=> Right side wins!
// alphabetWar("wwwwwwz"); //=> Left side wins!
//Solution
function alphabetWar(fight){
//set up an object with the left side and right side scores
let leftPoints = {
'w':4,
'p':3,
'b':2,
's':1
};
let rightPoints = {
'm':4,
'q':3,
'd':2,
'z':1
};
//set up variables to track the totals of each side
let leftScore = 0;
let rightScore = 0;
//loop through the input str and check if any of the letters exist in the score object and tally the sums
for(let i=0; i<fight.length; i++){
if(leftPoints[fight[i]]){
leftScore+=leftPoints[fight[i]]
}else if(rightPoints[fight[i]]){
rightScore+=rightPoints[fight[i]]
}
};
//check who wins and return the victor
if(leftScore > rightScore){
return "Left side wins!"
}else if(rightScore > leftScore){
return "Right side wins!"
}else{
//if noone wins return lets fight again
return "Let's fight again!"
}
}
//str -> str of letters, can be empty, wont be null or undefined, will always be a str of lowercase letters
//str -> if the left side letters wins return "Left side wins!", if the right side letters wins "Right side wins!" And if noone wins return "Let's fight again!"
console.log(alphabetWar("zzzzs"), "Right side wins!");