Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the smaller of two numbers.
DSA Typescript
function min(a: number, b: number): number { return a [1] b ? a : b; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' will return the larger number.
Using '==' will not compare sizes.
✗ Incorrect
The function returns 'a' if a is less than b, otherwise b. So the operator should be '<'.
2fill in blank
mediumComplete the code to calculate the remainder of division.
DSA Typescript
function remainder(a: number, b: number): number {
return a [1] b;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' returns the quotient, not remainder.
Using '*' or '-' are arithmetic but not for remainder.
✗ Incorrect
The '%' operator returns the remainder of dividing 'a' by 'b'.
3fill in blank
hardFix the error in the recursive GCD function to correctly call itself.
DSA Typescript
function gcd(a: number, b: number): number {
if (b === 0) {
return a;
}
return gcd([1], b);
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping arguments incorrectly.
Using subtraction instead of modulo.
✗ Incorrect
The Euclidean algorithm calls gcd with (b, a % b). Here, the first argument should be 'a % b' to reduce the problem.
4fill in blank
hardComplete the code to compute the LCM using GCD.
DSA Typescript
function lcm(a: number, b: number): number {
return (a [1] b) / gcd(a, b , 0);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*'.
Missing comma between gcd arguments.
✗ Incorrect
LCM is calculated as (a * b) divided by gcd(a, b). The gcd function needs both arguments separated by a comma.
5fill in blank
hardFill both blanks to complete the Euclidean GCD algorithm with recursion.
DSA Typescript
function gcd(a: number, b: number): number {
if (b [1] 0) {
return a;
}
return gcd(b, a [2] b);
}
const result = gcd(48, 18);
console.log(result , '');Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '==='.
Using '+' instead of '%'.
Using '+' instead of ',' in console.log.
✗ Incorrect
The base case checks if b === 0. The recursive call uses a % b. The console.log uses ',' to separate arguments.
