-
Notifications
You must be signed in to change notification settings - Fork 0
/
countLetters.js
40 lines (35 loc) · 1008 Bytes
/
countLetters.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
33
34
35
36
37
38
39
40
// assertEqual function
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`✔️Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`❌Assertion Failed: ${actual} !== ${expected}`);
}
};
// countLetters function
const countLetters = function(sentence) {
const letterCount = {};
for (const characters of sentence) {
if (characters !== ' ') {
if (letterCount[characters]) {
letterCount[characters] += 1;
} else {
letterCount[characters] = 1;
}
}
}
return letterCount;
};
// Testing the countLetters function
const result = countLetters('lighthouse in the house');
assertEqual(result['l'], 1);
assertEqual(result['i'], 2);
assertEqual(result['g'], 1);
assertEqual(result['h'], 4);
assertEqual(result['t'], 2);
assertEqual(result['o'], 2);
assertEqual(result['u'], 2);
assertEqual(result['s'], 2);
assertEqual(result['e'], 3);
assertEqual(result['n'], 1);
module.exports = countLetters;