Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Closures: javascript interview question

Problem

Write a function sum which can accept both the syntax.

sum(5, 3) // 8
sum(5)(3) // 8

Logic

  • Whenever the sum is called we need to detect if its called with two arguments of one.
  • If its called with two arguments then the problem is very simple
  • Else the sum function returns another function which has accepts one argument and it has access to the variable a via closure.

Solution

Reference


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *