Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the first term of the count and say sequence.
DSA Javascript
let result = "[1]";
Attempts:
3 left
💡 Hint
Common Mistakes
Using number 1 instead of string "1".
Starting with 0 or 2 instead of 1.
✗ Incorrect
The first term of the count and say sequence is the string "1".
2fill in blank
mediumComplete the code to iterate over the characters of the current term.
DSA Javascript
for (let i = 0; i < current.length; [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using i-- which causes infinite loop.
Incrementing by 2 which skips characters.
✗ Incorrect
To iterate over each character, increment i by 1 each loop with i++.
3fill in blank
hardFix the error in the code to correctly count consecutive characters.
DSA Javascript
if (current[i] === current[[1]]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 which looks at previous character.
Using i + 2 which skips one character.
✗ Incorrect
To check the next character, compare current[i] with current[i + 1].
4fill in blank
hardFill both blanks to build the next term by appending count and character.
DSA Javascript
nextTerm += count.toString() + [1]; count = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the wrong character like current[i + 1].
Resetting count to 0 instead of 1.
✗ Incorrect
Append the current character and reset count to 1 for the next group.
5fill in blank
hardFill all three blanks to complete the count and say function.
DSA Javascript
function countAndSay(n) {
let result = "1";
for (let [1] = 1; [1] < [3]; [1]++) {
let current = result;
let nextTerm = "";
let count = 1;
for (let i = 0; i < current.length; i++) {
if (current[i] === current[i + 1]) {
count++;
} else {
nextTerm += count.toString() + current[i];
count = 1;
}
}
result = nextTerm;
}
return [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop variable name.
Returning 'n' or 'count' instead of 'result'.
✗ Incorrect
Use 'i' as loop variable, return 'result', and loop until 'n'.
