Max Chars
How can you find the character count for sentences.
const sentence = "How are you doing today?";
const charCount = sentence
.toLowerCase()
.replace(/[^A-Za-z]/g, "")
.split("")
.reduce(
(final, letter) => ({ ...final, [letter]: final[letter] + 1 || 1 }),
{}
);
Now determine which character displays the most
let maxLetter = null;
let maxCount = 0;
for (const letter in charCount) {
const count = charCount[letter];
if (count > maxCount) {
maxLetter = letter;
maxCount = count;
}
}
const result = {maxLetter, maxCount};
And of course using reduce with the object’s keys
const max = Object.keys(charCount).reduce((mLetter, letter) => {
if (mLetter.count >= charCount[letter]) {
return mLetter;
}
return { count: charCount[letter], letter };
}, {});