logo
logo

Context with Apply

Beginner

Context with Apply

In this exercise, you're tasked with completing the implementation of the
sum()
function.
The function should accept no arguments and use the
apply()
method to add the values of the
this
array and return the result.
The
numbers
array should be passed as the
this
object when calling the
apply()
method on the
sum()
function.
Console
Submit
Solution
00:00

Solution Walkthrough for Context with Apply

By using
apply()
, we can pass an array as the context for the function execution. In this case, we pass the
numbers
array as the first argument to
apply()
, which sets
this
inside the
sum
function to the
numbers
array.
In the
sum
function, we access the first and second element of the
this
array using
[0]
and
[1]
respectively, and return their sum.