Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert a digit character to its integer value.
DSA Typescript
const digit = str.charCodeAt(0) - [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting '1' char code instead of '0'.
Using character 'a' char code which is unrelated.
✗ Incorrect
Subtracting the char code of '0' from the character code of a digit gives its integer value.
2fill in blank
mediumComplete the code to skip leading whitespace characters in the string.
DSA Typescript
while (i < str.length && str[i] === [1]) { i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for tab '\t' instead of space.
Checking for newline '\n' which is not leading space here.
✗ Incorrect
Leading spaces are skipped by checking for the space character ' '.
3fill in blank
hardFix the error in the condition to detect if the current character is a digit.
DSA Typescript
if (str[i] >= [1] && str[i] <= [2]) { num = num * 10 + (str.charCodeAt(i) - [3]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using char codes for comparison instead of characters.
Subtracting '9' char code instead of '0'.
✗ Incorrect
Check if character is between '0' and '9' and subtract char code of '0' to get digit value.
4fill in blank
hardFill both blanks to correctly handle the sign of the number.
DSA Typescript
if (str[i] === [1]) { sign = -1; i++; } else if (str[i] === [2]) { sign = 1; i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing '+' and '-' characters.
Not incrementing index after sign.
✗ Incorrect
Minus sign sets sign to -1, plus sign sets sign to 1 and both advance index.
5fill in blank
hardFill all three blanks to clamp the number within 32-bit signed integer range.
DSA Typescript
if (num * sign < [1]) return [1]; if (num * sign > [2]) return [2]; return num * sign;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as clamp limits instead of 32-bit int limits.
Not clamping at all.
✗ Incorrect
Clamp to minimum -2147483648 and maximum 2147483647 for 32-bit signed integer.
