-
Notifications
You must be signed in to change notification settings - Fork 0
/
If_You_Can_Read_This.js
42 lines (32 loc) · 1.73 KB
/
If_You_Can_Read_This.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
// ----------------------------------Description---------------------------
// You'll have to translate a string to Pilot's alphabet (NATO phonetic alphabet).
// Input:
// If, you can read?
// Output:
// India Foxtrot , Yankee Oscar Uniform Charlie Alfa November Romeo Echo Alfa Delta ?
// Note:
// There is a preloaded dictionary that you can use, named NATO. It uses uppercase keys, e.g. NATO['A'] is "Alfa". See comments in the initial code to see how to access it in your language.
// The set of used punctuation is ,.!?.
// Punctuation should be kept in your return string, but spaces should not.
// Xray should not have a dash within.
// Every word and punctuation mark should be seperated by a space ' '.
// There should be no trailing whitespace
// ------------------------------------------------------------------------
function toNato(words) {
const NATO = {
'A': 'Alfa', 'B': 'Bravo', 'C': 'Charlie', 'D': 'Delta', 'E': 'Echo',
'F': 'Foxtrot', 'G': 'Golf', 'H': 'Hotel', 'I': 'India', 'J': 'Juliett',
'K': 'Kilo', 'L': 'Lima', 'M': 'Mike', 'N': 'November', 'O': 'Oscar',
'P': 'Papa', 'Q': 'Quebec', 'R': 'Romeo', 'S': 'Sierra', 'T': 'Tango',
'U': 'Uniform', 'V': 'Victor', 'W': 'Whiskey', 'X': 'Xray', 'Y': 'Yankee',
'Z': 'Zulu', ',': ',', '.': '.', '?': '?', '!': '!'
};
const translatedStr = words.split(/\s+/).map(word => {
return word.split('').map(char => {
const upperChar = char.toUpperCase();
return NATO.hasOwnProperty(upperChar) ? NATO[upperChar] : char;
}).join(' ');
}).join(' ');
return translatedStr;
}
console.log('result', toNato("If, you can read?")) //India Foxtrot , Yankee Oscar Uniform Charlie Alfa November Romeo Echo Alfa Delta ?