Skip to content

Commit afd1b11

Browse files
committed
refactor: copy words before sorting
1 parent 8f51e2a commit afd1b11

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/word-utils.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export function isAllCapitals(word) {
22
return /^[A-Z]+$/.test(word);
33
}
4+
45
/**
56
* Capital Word to Acronym
67
* @param {string} CapitalWord
@@ -23,26 +24,28 @@ export function expandOneWordToAcronym(CapitalWord) {
2324
}
2425
return acronym;
2526
}
27+
2628
/*
2729
* create Acronym from words.
2830
* @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
3032
*/
3133
export function expandWordsToAcronym(words) {
3234
//XMLHttpRequest -> XHR
3335
if (words.length === 1) {
3436
return [expandOneWordToAcronym(words[0])];
3537
} else {
3638
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) => {
3942
acronym.unshift(word.charAt(0));
4043
result.push(acronym.join(""));
4144
return acronym;
4245
}, []);
4346

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) => {
4649
acronym.push(word.charAt(0));
4750
result.push(acronym.join(""));
4851
return acronym;

0 commit comments

Comments
 (0)