const str = 'CoNgratulations';
const letters = {};
for (let char of str.toLowerCase()) {
letters[char] = letters[char] + 1 || 1;
}
console.log(letters);
In the code snippet, there is string str which contains some characters in it. We have converted the string to lowecase so that it does now confuse with capital letters and return the right rest.
We are looping through the string characters and adding 1 in each iteration in the letters object.
0 Comments