Complete the code to initialize an empty object to store character counts.
const freq = [1];We use an empty object {} to store character frequencies as key-value pairs.
Complete the code to loop through each character in the string.
for (const char [1] str) {
Use for...of to iterate over characters in a string.
Fix the error in updating the frequency count for each character.
freq[char] = (freq[char] [1] 0) + 1;
The || operator returns the existing count or 0 if undefined, then adds 1.
Fill both blanks to create a function that returns the frequency object for a given string.
function charFreq(str) {
const freq = [1];
for (const char [2] str) {
freq[char] = (freq[char] || 0) + 1;
}
return freq;
}Initialize freq as an empty object {} and loop over characters using for...of.
Fill all three blanks to create a function that returns a formatted string showing character counts.
function formatFreq(str) {
const freq = charFreq(str);
let result = '';
for (const [1] in freq) {
result += [2] + freq[[3]] + '\n';
}
return result;
}Loop over keys with char, append char + ' -> ' plus count, and access count with freq[char].
