Skip to content

Commit c6e8a79

Browse files
committed
Refactor 1.2 solution with reduce logic
1 parent 6d54def commit c6e8a79

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

JavaScript/chapter01/1.2 - Check Perm/rroque98_sol.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ const isPermutation = (str1, str2) =>{
1515
return true;
1616
//******** helper function ******
1717
function determineCharCount(string) {
18-
const obj = {};
19-
for (let char of string) {
20-
if (obj[char] !== undefined) {
21-
obj[char]++;
18+
return string.split('').reduce((acc, char) => {
19+
if (acc[char] !== undefined) {
20+
acc[char]++;
2221
} else {
23-
obj[char] = 1;
22+
acc[char] = 1;
2423
}
25-
}
26-
return obj;
24+
return acc;
25+
}, {});
2726
}
2827
}
2928

0 commit comments

Comments
 (0)