Javascript Developer JSE 40 01 · Free Practice Question Medium

Question 1

Q429 - Functions


Analyze the following code:



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
  • B
  • C
  • D
Reveal correct answer

Correct answer: C

Explanation

Topics: arrow function function declaration


Try it yourself:


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.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need