We want:
const addByEight = addBy(8);
const sum = addByEight(50);
And we need sum to be 58.
That means:
addBy(8) must return a function.
That returned function, when called with 50, must compute 8 + 50.
So addBy must be a higher-order function that returns another function capturing num1 and later using it with num2 (a closure).
const addBy = function(num1) {
return function(num2) {
return num1 + num2;
}
}
Step-by-step:
Call addBy(8):
num1 is 8.
The function returns an inner function: function(num2) { return num1 + num2; }.
So addByEight becomes this inner function, with num1 closed over as 8.
Then call addByEight(50):
num2 is 50.
The body computes num1 + num2, i.e., 8 + 50 = 58.
Therefore, with Option A in place:
const addByEight = addBy(8); // returns inner function
const sum = addByEight(50); // 58
So sum is 58. Option A is correct.
const addBy = (num1) = > {
return function(num2) {
return num1 + num2;
}
}
This is essentially the same logic expressed with an arrow function for the outer function:
So again:
const addByEight = addBy(8); // inner function with num1 = 8
const sum = addByEight(50); // 58
Option D is also correct.
Thus, A and D are the two functions that satisfy the requirement.
Option B:
const addBy = function(num1) {
return num1 * num2;
}
This does not return a function; it returns a value.
addBy(8) returns 8 * num2, but num2 is not defined in this scope, which would cause a ReferenceError.
Also, addByEight would be a number (if num2 existed), not a function, so addByEight(50) would fail.
Option C:
const addBy = (num1) = > num1 + num2;
Again, addBy returns a value, not a function.
addBy(8) returns 8 + num2, but num2 is not defined, so this is also invalid due to ReferenceError.
addByEight would be a number (or error), not a function.
Neither B nor C creates the required closure nor returns a function to be called later.
References / Study Guide concepts (no links):
Higher-order functions in JavaScript
Closures: inner functions capturing outer variables (num1)
Arrow functions vs function expressions
Returning functions from functions (function factories / currying)
Scope and ReferenceError when a variable is not defined