-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacterFrequency.js
More file actions
26 lines (22 loc) · 966 Bytes
/
Copy pathcharacterFrequency.js
File metadata and controls
26 lines (22 loc) · 966 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
// Description
// Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character!
// Explanation
// Your function will be called char_freq/charFreq/CharFreq and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input.
// Example
// charFreq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1}
function charFreq(message) {
return message.split("").reduce((total, letter) => {
total[letter] ? total[letter]++ : (total[letter] = 1);
return total;
}, {});
}
function charFreq(message) {
let arr = message.split("");
let item = {};
for (let i = 0; i < arr.length; i++) {
if (!item[arr[i]]) {
item[arr[i]] = 1;
} else item[arr[i]]++;
}
return item;
}