Complete the code to call the function named greet.
function greet() {
console.log('Hello!');
}
greet[1];To call a function in JavaScript, you use parentheses () after the function name.
Complete the code to log the value returned by the function add.
function add(a, b) {
return a + b;
}
console.log(add[1]);Functions are called with parentheses and arguments inside, separated by commas.
Fix the error in the recursive function call to avoid infinite recursion.
function countdown(n) {
if (n <= 0) {
console.log('Done');
return;
}
console.log(n);
countdown[1];
}n + 1 causing infinite recursion.To count down, the function must call itself with n - 1 inside parentheses.
Fill both blanks to create a function that returns the sum of squares of numbers in an array.
function sumSquares(arr) {
return arr.reduce((acc, num) => acc [1] num[2] 2, 0);
}The reduce function adds (+) the square (** 2) of each number.
Fill all three blanks to create a recursive function that calculates factorial of n.
function factorial([1]) { if ([2] <= 1) { return 1; } return [3] * factorial(n - 1); }
The function uses n as parameter, checks if n <= 1, and returns n * factorial(n - 1).
