1
1
export function isAllCapitals ( word ) {
2
2
return / ^ [ A - Z ] + $ / . test ( word ) ;
3
3
}
4
+
4
5
/**
5
6
* Capital Word to Acronym
6
7
* @param {string } CapitalWord
@@ -23,26 +24,28 @@ export function expandOneWordToAcronym(CapitalWord) {
23
24
}
24
25
return acronym ;
25
26
}
27
+
26
28
/*
27
29
* create Acronym from words.
28
30
* @param {string[] } words
29
- * @returns (1) string if only one word (2) array if multiple words
31
+ * @returns { string[] } string if only one word (2) array if multiple words
30
32
*/
31
33
export function expandWordsToAcronym ( words ) {
32
34
//XMLHttpRequest -> XHR
33
35
if ( words . length === 1 ) {
34
36
return [ expandOneWordToAcronym ( words [ 0 ] ) ] ;
35
37
} else {
36
38
const result = [ ] ;
37
- //In American Broadcast Company -> ["C", "BC", "ABC", "IABC"]
38
- words . reverse ( ) . reduce ( ( acronym , word , i ) => {
39
+ // In American Broadcast Company -> ["C", "BC", "ABC", "IABC"]
40
+ const reversedWords = words . slice ( ) . reverse ( ) ;
41
+ reversedWords . reduce ( ( acronym , word , i ) => {
39
42
acronym . unshift ( word . charAt ( 0 ) ) ;
40
43
result . push ( acronym . join ( "" ) ) ;
41
44
return acronym ;
42
45
} , [ ] ) ;
43
46
44
- //In American Broadcast Company -> ["I", "IA", "IAB", "IABC"]
45
- words . reverse ( ) . reduce ( ( acronym , word , i ) => {
47
+ // In American Broadcast Company -> ["I", "IA", "IAB", "IABC"]
48
+ words . reduce ( ( acronym , word , i ) => {
46
49
acronym . push ( word . charAt ( 0 ) ) ;
47
50
result . push ( acronym . join ( "" ) ) ;
48
51
return acronym ;
0 commit comments