JSFiddle - React, Tailwind, and code Playground
JavaScript
// Count each character in a sentence
const str = "Hello World";
// function countChars() {
// let freq = new Map();
// for(let char of str) {
// if(freq.has(char)) {
// freq[char] = (char || 0) + 1;
// }
// freq[char] = 1;
// }
// console.log(freq)
// }
function countChars() {
let freq = {};
for(let char of str) {
freq[char] = (freq[char] || 0) + 1;
}
console.log(freq);
}
countChars();