Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the function named greet.
Javascript
function greet() {
console.log('Hello!');
}
greet[1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses.
Forgetting to add parentheses, so the function is not called.
✗ Incorrect
To call a function in JavaScript, you use parentheses () after the function name.
2fill in blank
mediumComplete the code to log the value returned by the function add.
Javascript
function add(a, b) {
return a + b;
}
console.log(add[1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or curly braces instead of parentheses.
Passing arguments without parentheses.
✗ Incorrect
Functions are called with parentheses and arguments inside, separated by commas.
3fill in blank
hardFix the error in the recursive function call to avoid infinite recursion.
Javascript
function countdown(n) {
if (n <= 0) {
console.log('Done');
return;
}
console.log(n);
countdown[1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling with
n + 1 causing infinite recursion.Forgetting parentheses around the argument.
✗ Incorrect
To count down, the function must call itself with n - 1 inside parentheses.
4fill in blank
hardFill both blanks to create a function that returns the sum of squares of numbers in an array.
Javascript
function sumSquares(arr) {
return arr.reduce((acc, num) => acc [1] num[2] 2, 0);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of addition in reduce.
Using wrong operator for squaring.
✗ Incorrect
The reduce function adds (+) the square (** 2) of each number.
5fill in blank
hardFill all three blanks to create a recursive function that calculates factorial of n.
Javascript
function factorial([1]) { if ([2] <= 1) { return 1; } return [3] * factorial(n - 1); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Missing base case or wrong condition.
✗ Incorrect
The function uses n as parameter, checks if n <= 1, and returns n * factorial(n - 1).