-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hex Word Sum
27 lines (18 loc) · 848 Bytes
/
Hex Word Sum
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
/*
Description
As hex values can include letters A through to F, certain English words can be spelled out, such as CAFE, BEEF, or FACADE. This vocabulary can be extended by using numbers to represent other letters, such as 5EAF00D, or DEC0DE5.
Given a string, your task is to return the decimal sum of all words in the string that can be interpreted as such hex values.
Example
Working with the string "BAG OF BEES":
"BAG" = 0, as it is not a valid hex value
"OF" = 0F = 15
"BEES" = BEE5 = 48869
So the result is the sum of these: 48884 (0 + 15 + 48869)
Notes
Inputs are all uppercase and contain no punctuation
0 can be substituted for O
5 can be substituted for S
*/
export function hexWordSum(string: string): number {
return string.replace(/O/g,'0').replace(/S/g,'5').split(' ').reduce((a,c)=>a + (+('0x'+ c) || 0),0);
}