-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountLetters.js
More file actions
33 lines (28 loc) · 942 Bytes
/
countLetters.js
File metadata and controls
33 lines (28 loc) · 942 Bytes
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
const assertEqual = require('./assertEqual');
//The function should take in a sentence (as a string)
//and then return a count of each of the letters in that sentence.
const countLetters = function(sentence){
//results count instances of letter
let result = {};
let count = 0;
//loop through each letter in string
for (let letter of sentence){
//if it is not a space move on
if (letter !== " "){
//does this letter already exist in the results object?
if(result[letter]){
//if it exists already add one to the existing count
count = result[letter];
result[letter] = count + 1;
} else{
//if it doesn't already exist, set count to one
result[letter] = 1;
}
}
}
console.log(result);
return result;
}
module.exports = countLetters;
// countLetters("LHL");
// countLetters("lighthouse in the house")