📖 Scenario: Imagine you are building a simple tool that counts how many times each letter appears in a word. This can help in games like Scrabble or in analyzing text.
🎯 Goal: You will create a program that counts the frequency of each character in a given word and shows the result as a dictionary-like object.
📋 What You'll Learn
Create a string variable with a specific word
Create an empty object to store character counts
Use a for loop to count each character's frequency
Print the final frequency object
💡 Why This Matters
🌍 Real World
Counting character frequency is useful in text analysis, games, and data compression.
💼 Career
Understanding how to count and store frequencies helps in programming tasks like parsing, searching, and building efficient algorithms.
Progress0 / 4 steps
1
Create the word string
Create a string variable called word and set it to the exact value "balloon".
DSA Javascript
Hint
Use const to create the variable word and assign the string "balloon".
2
Create an empty object for counts
Create an empty object called charCounts to store the frequency of each character.
DSA Javascript
Hint
Use {} to create an empty object and assign it to charCounts.
3
Count characters using a for loop
Use a for loop with the variable char to go through each character in word. Inside the loop, increase the count of char in charCounts by 1, or set it to 1 if it does not exist yet.
DSA Javascript
Hint
Use for (const char of word) to loop through each character. Use an if to check if charCounts[char] exists.
4
Print the character counts
Print the charCounts object using console.log(charCounts) to show the frequency of each character.
DSA Javascript
Hint
Use console.log(charCounts) to print the object showing character counts.
Practice
(1/5)
1. What does character frequency counting mean in simple terms?
easy
A. Sorting characters alphabetically
B. Counting how many times each character appears in a string
C. Replacing characters with numbers
D. Removing duplicate characters from a string
Solution
Step 1: Understand the meaning of frequency
Frequency means how often something appears.
Step 2: Apply to characters in a string
Counting frequency means counting how many times each character shows up in the string.
Final Answer:
Counting how many times each character appears in a string -> Option B
Quick Check:
Frequency counting = Counting characters [OK]
Hint: Frequency means count occurrences of each character [OK]
Common Mistakes:
Confusing frequency with sorting
Thinking frequency means removing duplicates
Mixing frequency with character replacement
2. Which of the following JavaScript code snippets correctly initializes an empty object to store character frequencies?
easy
A. const freq = '';
B. const freq = [];
C. const freq = {};
D. const freq = null;
Solution
Step 1: Identify the data structure for frequency counting
We use an object to store characters as keys and counts as values.
Step 2: Check correct syntax for empty object
Empty object is created with curly braces {} in JavaScript.
Final Answer:
const freq = {}; -> Option C
Quick Check:
Empty object = {} [OK]
Hint: Use curly braces {} to create empty object [OK]
A. Loop condition should be i < str.length, not i <= str.length
B. freq should be initialized as an array, not object
C. Use let instead of const for freq
D. Missing semicolon after freq[ch] assignment
Solution
Step 1: Check loop range
Loop runs from i = 0 to i <= str.length, which means i goes up to str.length (3 for 'abc').
Step 2: Identify out-of-bounds access
str[3] is undefined, so freq[undefined] is updated causing wrong key.
Final Answer:
Loop condition should be i < str.length, not i <= str.length -> Option A
Quick Check:
Array index max is length - 1 [OK]
Hint: Use i < length to avoid out-of-bounds errors [OK]
Common Mistakes:
Using <= instead of < in loop
Initializing freq as array instead of object
Ignoring undefined character access
5. Given the string "aabbccddeeffgghhii", which code snippet correctly counts only characters that appear more than once and stores them in freq object?
hard
A. const freq = {};
for (const ch of str) {
freq[ch] = (freq[ch] || 0) + 1;
}
const filtered = {};
for (const ch in freq) {
if (freq[ch] > 1) filtered[ch] = freq[ch];
}
B. const freq = {};
for (const ch of str) {
if (freq[ch]) freq[ch]++;
else freq[ch] = 1;
}
for (const ch in freq) {
if (freq[ch] < 2) freq[ch] = 0;
}
C. const freq = {};
for (const ch of str) {
freq[ch] = 1;
}
for (const ch in freq) {
if (freq[ch] > 1) delete freq[ch];
}
D. const freq = {};
for (const ch of str) {
freq[ch] = (freq[ch] || 0) + 1;
}
for (const ch in freq) {
if (freq[ch] <= 1) delete freq[ch];
}
Solution
Step 1: Count all character frequencies
Use freq[ch] = (freq[ch] || 0) + 1 to count all characters.
Step 2: Delete characters with frequency <= 1 from freq
Iterate and delete keys where freq[ch] <= 1 to leave only duplicates in freq.
Final Answer:
Code snippet that counts all then deletes singles from freq -> Option D
Quick Check:
Delete freq <= 1 from freq [OK]
Hint: Count all, then delete characters with count <= 1 [OK]
Common Mistakes:
Deleting keys while iterating freq object
Setting frequency to 0 instead of removing
Overwriting counts incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.