Problem
Implement add so you can chain calls like add(1)(2)(3)(4) and get the total sum.
Support both ways to finish the chain:
- Unary plus coercion:
+add(1)(2)(3)(4)→10 - Empty call:
add(1)(2)(3)()→6
Each call adds its argument to a running total and returns a function that accepts the next number.
Input
+add(1)(2)(3)(4);
add(1)(2)(3)();
Expected output
- First expression:
10 - Second expression:
6
Implement:
function add(a) {
// Your code here
}