-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtask5.js
More file actions
35 lines (32 loc) · 1.03 KB
/
task5.js
File metadata and controls
35 lines (32 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Create a single function which can perform sum(a, b, c), sum(a, b)(c), sum(a)(b, c) and sum(a)(b)(c) and returns sum of a, b and c
// Example
// sum(2)(4)(6); // 12
// sum(3, 2)(5); // 10
// sum(4)(-10, -6); // -12
// sum(6, -3, 1); // 4
// Sachin
function sum(a, b, c) {
if (a != undefined && b != undefined && c != undefined) { // of type sum(a,b,c)
return a + b + c;
} else if (a != undefined && b != undefined && c == undefined) { // of type sum(a,b)(c)
return function (c) {
return a + b + c;
};
} else if (a != undefined && b == undefined && c == undefined) {
return function (b, c) {
if (b != undefined) {
if (c != undefined) { // of type sum(a)(b,c)
return a + b + c;
} else {
return function (c) { // of type sum(a)(b)(c)
if (c != undefined) return a + b + c;
};
}
}
};
}
}
console.log(sum(2)(4)(6));
console.log(sum(3, 2)(5));
console.log(sum(4)(-10, -6));
console.log(sum(6, -3, 1));