-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterCounting.js
41 lines (33 loc) · 921 Bytes
/
CharacterCounting.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
41
function CountLetters(strings) {
/*
*
* Given an array of strings, returns an object
* whose keys are the number of characters in the strings
* and the values are the number of letters with each length.
* If the string is empty, doesn't count it.
*
* @return {object}
*/
// 1. Initialize a return value
var CharacterCount = {};
// 2. Loop over the input
for (var i = 0; i < strings.length; i++) {
var string = strings[i];
if (string === ' ') {
//Don't worry about it
continue;
}
//var countLetters = string.length;
else if (CharacterCount [string] === undefined) {
CharacterCount[string] = 1;
}
// 3. Inside the loop, modify the return value
else {
CharacterCount[string] =+ 1;
}
}
// 4. Return the return value
return CharacterCount;
}
console.log(CountLetters(['aaaaaaaaaakdfjlksjf']))
console.log(CountLetters('lighthouse in the house'))