Javascript Developer JSE 40 01 · Free Practice Question Medium
Question 1
Q429 - Functions
Analyze the following code:
- function executeOperation(a, b, op) {
- return op(a, b);
- }
- console.log(executeOperation(10, 20, x));
Before declaring the function, we should add one more line of code.
Which line should we add, if the execution of the completed code
results in the console displaying the value 30?
-
A
- let x = 10 + 20;
-
B
- let x = function () {
- return a + b;
- };
-
C
- let x = (a, b) => a + b;
-
D
- let x = 30;
Reveal correct answer
Correct answer: C
Explanation
Topics: arrow function function declaration
Try it yourself:
- let x1 = (a, b) => a + b;
- function executeOperation1(a, b, op) {
- return op(a, b);
- }
- console.log(executeOperation1(10, 20, x1)); // 30
- let x2 = 30;
- function executeOperation2(a, b, op) {
- return op(a, b);
- }
- // console.log(executeOperation2(10, 20, x2));
- // Uncaught TypeError: op is not a function
- let x3 = 10 + 20;
- function executeOperation3(a, b, op) {
- return op(a, b);
- }
- // console.log(executeOperation3(10, 20, x3));
- // Uncaught TypeError: op is not a function
- let x4 = function () {
- return a + b;
- };
- function executeOperation4(a, b, op) {
- return op(a, b);
- }
- console.log(executeOperation4(10, 20, x4));
- // Uncaught ReferenceError: a is not defined
Explanation:
x needs to be a function because it is passed to the op parameter
and then called inside of executeOperation()
x needs to expect the two arguments 10 and 20 and therefore have two parameters.
x needs to add the two parameters and return the sum.
Therefore the only possible solution is:
let x = (a, b) => a + b;
Arrow functions allow us to write shorter function syntax.
https://www.w3schools.com/js/js_arrow_function.asp
The function declaration (function statement) defines a function.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function
(There is a similar question Q230.)
Q429 (Please refer to this number, if you want to write me about this question.)
A. This choice assigns the sum of 10 and 20 to the variable x. However, x should be a function that takes two parameters and returns their sum, not just a static value. When executeOperation(10, 20, x) is called, it will try to execute 30(10, 20), which will result in an error. Therefore, this choice is incorrect as it does not provide a valid function definition for x.
B. This choice defines a function expression x without parameters that returns the sum of a and b. However, the function should take two parameters a and b to align with the op parameter in executeOperation. When executeOperation(10, 20, x) is called, it will try to execute x(10, 20), which will result in an error because x does not accept any arguments. Therefore, this choice is incorrect as it does not provide a valid function definition for x in the context of the code snippet.
C. This choice correctly defines a function expression x that takes two parameters a and b and returns their sum. When executeOperation(10, 20, x) is called, it will pass 10 and 20 as arguments to the function x, resulting in the sum of 10 and 20, which is 30. This choice is correct as it aligns with the expected behavior of the code snippet.
D. This choice assigns the value 30 to the variable x, which is not a function. When executeOperation(10, 20, x) is called, it will try to execute 30(10, 20), which will result in an error because 30 is not a function. Therefore, this choice is incorrect as it does not align with the requirement of passing a function as the op parameter in executeOperation.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
