-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount-characters-in-your-string.js
More file actions
51 lines (35 loc) · 1.32 KB
/
Count-characters-in-your-string.js
File metadata and controls
51 lines (35 loc) · 1.32 KB
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
42
43
44
45
46
47
48
49
50
//6 Kyu
//Count characters in your string
//Fundamentals, strings
// The main idea is to count all the occurring characters in a string. If you have a string like aba, then the result should be {'a': 2, 'b': 1}.
// What if the string is empty? Then the result should be empty object literal, {}.
//Solution I
function count(string) {
//create an empty object
let letterObj = {}
//normalize the string, sort, toLowerCase, and split it to an array of letters
let normStr = string.split('')
//loop through array and create a property named for each letter you loop
//if its the first time make the property equal to one if not then add one to it
normStr.map((letter)=>letterObj[letter]=letterObj[letter]+1||1)
//return object
return letterObj;
}
// Solution II
function count (string) {
//first we set up an empty obj to define it with our letters
let count = {};
//we lopp thorugh the split string array
string.split('').forEach(function(letter) {
//If count
count[letter] ? count[letter]++ : count[letter] = 1;
});
return count;
}
//Parameters
//input-> strings, empty strings are okay, keep Cap letters, keep order.
//return
//object with one property value pair for each string elements/letter, not counting spaces.
//Examples
//console.log(count('maxa',{a:2,m:1,x:2})
//console.log(count("ab"),{a:1,b:1})