A developer implements a function that adds a few values.
function sum(num1, num2, num3) {
if (num3 === undefined) {
num3 = 0;
}
return num1 + num2 + num3;
}
Which three options can the developer invoke for this function to get a return value of 10?
Correct Answer: C,D,E
The verified corrected answers are C, D, and E.
This function is a normal function, not a curried function:
function sum(num1, num2, num3) {
if (num3 === undefined) {
num3 = 0;
}
return num1 + num2 + num3;
}
It expects values to be passed in the same function call:
sum(num1, num2, num3);
Now check the valid corrected options.
Option C:
sum(5, 5, 0);
Calculation:
5 + 5 + 0
Result:
10
So C is correct.
Option D:
sum(10, 0);
Here, num3 is not provided, so it is undefined.
The function checks:
if (num3 === undefined) {
num3 = 0;
}
So the calculation becomes:
10 + 0 + 0
Result:
10
So D is correct.
Option E:
sum(5, 2, 3);
Calculation:
5 + 2 + 3
Result:
10
So E is correct.
Why A and B are incorrect as originally written:
sum(5)(5);
This calls sum(5) first. Since num2 is missing, the result becomes NaN. Then JavaScript tries to call that returned value as a function, which causes a TypeError.
sum()(10);
This also calls sum() first, producing NaN, and then attempts to call NaN as a function.
Those styles would only work if sum were written as a curried function, but the given implementation is not curried.
Therefore, the verified corrected answers are C, D, and E.