Skip to content

Commit 8626792

Browse files
committed
--update: added max char problem
1 parent 29ce2ec commit 8626792

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Diff for: src/maxchar/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function findMaxchar(str) {
2+
if (typeof str !== 'string') {
3+
throw new Error('Invalid Argument');
4+
}
5+
6+
const charMap = {};
7+
const lowerCasedString = str.toLowerCase();
8+
9+
lowerCasedString.split('').forEach((char) => {
10+
if (!charMap[char]) {
11+
charMap[char] = 1;
12+
} else {
13+
charMap[char] += 1;
14+
}
15+
});
16+
17+
// find the char with highest score
18+
let max = charMap[lowerCasedString[0]];
19+
let char = lowerCasedString[0];
20+
21+
Object.keys(charMap).forEach((e) => {
22+
if (charMap[e] > max) {
23+
max = charMap[e];
24+
char = e;
25+
}
26+
});
27+
28+
return char;
29+
}
30+
31+
module.exports = {
32+
findMaxchar,
33+
};

Diff for: src/maxchar/maxchar.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { findMaxchar } = require('.');
2+
3+
describe('MaxChar', () => {
4+
it('Should throw error for invalid argument', () => {
5+
expect(() => findMaxchar(21)).toThrow('Invalid Argument');
6+
});
7+
8+
it('Should return `l` for given string `Hello World`', () => {
9+
expect(findMaxchar('Hello World')).toEqual('l');
10+
});
11+
12+
it('Should return `a` for given string `A wonderful day`', () => {
13+
expect(findMaxchar('A wonderful day')).toEqual('a');
14+
});
15+
});

0 commit comments

Comments
 (0)