What is your hint or solution suggestion?
First, split and sort the given string. Then, create an object where the keys are equal to the characters, and the values are equal to the number of times the characters are present in the string. Use Array.reduce
to create this object. Finally, use Object.entries
to convert the object to an array.
Solution 1
function letterFrequency(txt) {
return Object.entries(counter(txt));
}
let counter = str => {
return str.split('').sort().reduce((total, letter) => {
total[letter] ? total[letter]++ : total[letter] = 1;
return total;
}, {});
};
Challenge: Letter frequency
Link to the challenge: