-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacronym.js
32 lines (22 loc) · 996 Bytes
/
acronym.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
/*
**Problem**
Write a function that generates and returns an acronym from a string of words.
For example, the function should return "PNG" for the string "Portable Network Graphics".
Count compound words (words connected with a dash) as separate words.
**Examples / Test Cases**
**Data Structures**
**Algorithm**
1. split string by spaces and -
2. loop through array
- add first letter of each element to new string and capitalize
3. return new string
*/
function acronym(string) {
return string.split(/[ -]/)
.reduce((acronym, word) => acronym + word[0].toUpperCase(), '');
}
console.log(acronym('Portable Network Graphics')); // "PNG"
console.log(acronym('First In, First Out')); // "FIFO"
console.log(acronym('PHP: HyperText Preprocessor')); // "PHP"
console.log(acronym('Complementary metal-oxide semiconductor')); // "CMOS"
console.log(acronym('Hyper-text Markup Language')); // "HTML"