-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsonant-value
More file actions
23 lines (16 loc) · 1.21 KB
/
Copy pathconsonant-value
File metadata and controls
23 lines (16 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Codewars 6kyu Consonant value
Given a lowercase string that has alphabetic characters only and no spaces, return the highest value of consonant substrings.Consonants are any letters of the alphabet except "aeiou".
We shall assign the following values: a = 1, b = 2, c = 3, ....z = 26.
For example, for the word "zodiacs", let's cross out the vowels. We get: "z o d ia cs"
--The consonant substrings are: "z", "d" and "cs" and the values are z = 26, d = 4 and cs = 3 + 19 = 22. The highest is 26.
solve("zodiacs") = 26
For the word "strength", solve("strength") = 57
--The consonant substrings are: "str" and "ngth" with values "str" = 19 + 20 + 18 = 57 and "ngth" = 14 + 7 + 20 + 8 = 49. The highest is 57.
For C: do not mutate input.
More examples in test cases.Good luck!
SOLUTION
// s.match + regex / [^ aeiou] + / will return an array of substrings of non-vowels, with (+) checking for extra matches
// map will loop through the substrings in the new array returned and get the sum of all Unicode characters of each substring and then Math.max will return the maximum substring Unicode sum
function solve(s) {
return Math.max(...s.match(/[^aeiou]+/g).map(x => [...x].reduce((acc, c) => acc + c.charCodeAt() - 96, 0)))
};