Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an empty object to count frequencies.
DSA Javascript
const frequency = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using [] which creates an array instead of an object.
Using null or 0 which are not objects.
✗ Incorrect
An empty object {} is used to store frequency counts as key-value pairs.
2fill in blank
mediumComplete the code to check if a character exists in the frequency object.
DSA Javascript
if (frequency.hasOwnProperty([1])) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal 'char' instead of the variable char.
Using unrelated variables like frequency or i.
✗ Incorrect
Use the variable 'char' to check if that character is a key in the frequency object.
3fill in blank
hardFix the error in incrementing the frequency count for a character.
DSA Javascript
frequency[char] [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which overwrites the count instead of incrementing.
Using '-=' which decreases the count.
Using '++' incorrectly in this context.
✗ Incorrect
Use '+=' to add 1 to the existing count for the character key.
4fill in blank
hardFill both blanks to complete the loop that counts character frequencies.
DSA Javascript
for (let [1] = 0; [2] < str.length; [1]++) { const char = str[[1]]; // count logic here }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in initialization and condition.
Using 'length' instead of 'str.length' in the condition.
✗ Incorrect
Use 'i' as the loop variable consistently for initialization and condition.
5fill in blank
hardFill all three blanks to create a frequency counter function.
DSA Javascript
function frequencyCounter(str) {
const frequency = [1];
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (frequency.hasOwnProperty(char)) {
frequency[char] [2] 1;
} else {
frequency[char] = [3];
}
}
return frequency;
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing frequency as an array or number.
Using '=' instead of '+=' to increment counts.
Setting new keys to 0 instead of 1.
✗ Incorrect
Initialize frequency as an empty object {}, increment counts with '+=', and set new keys to 1.
