Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an empty object to store character counts.
DSA Javascript
const charCount = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using [] creates an array, not an object.
Using null or empty string does not create a usable object.
✗ Incorrect
We use an empty object {} to store counts of characters as keys.
2fill in blank
mediumComplete the code to loop through each character in the string 's'.
DSA Javascript
for (const [1] of s) { // count characters }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' or 'i' works but is less clear here.
Using 'element' is vague; 'char' is clearer.
✗ Incorrect
We use 'char' as the variable name to represent each character in the string.
3fill in blank
hardFix the error in updating the count of each character in the object.
DSA Javascript
charCount[char] = (charCount[char] [1] 0) + 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' alone causes NaN if charCount[char] is undefined.
Using '-' or '&&' does not provide a default value.
✗ Incorrect
We use '||' to default to 0 if charCount[char] is undefined, then add 1.
4fill in blank
hardFill both blanks to check if a character count is exactly 1 and return its index.
DSA Javascript
for (let i = 0; i < s.length; i++) { if (charCount[s[i]] [1] 1) { return [2]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!==' checks for not equal, which is wrong here.
Returning the character instead of index does not match problem requirements.
✗ Incorrect
We check if count equals 1 using '===' and return the index 'i'.
5fill in blank
hardFill all three blanks to complete the function that returns the first non-repeating character's index or -1 if none.
DSA Javascript
function firstUniqChar(s) {
const charCount = [1];
for (const char of s) {
charCount[char] = (charCount[char] [2] 0) + 1;
}
for (let i = 0; i < s.length; i++) {
if (charCount[s[i]] === 1) {
return [3];
}
}
return -1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using [] instead of {} for charCount.
Using '+' instead of '||' causing NaN errors.
Returning the character instead of index.
✗ Incorrect
We create an empty object {}, use '||' to default counts, and return index i.
