Created
March 5, 2019 10:39
-
-
Save abhinavnigam2207/8f9e8b5774c0b81a21f780fde9c5c0c0 to your computer and use it in GitHub Desktop.
Make the below code work (Asked in amazon interview)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Make the below function calling work: | |
myNumber(10) | |
.subtract(3) | |
.add(10,1,3) | |
.value(); | |
myNumber(10) | |
.add(7) | |
.value(); | |
*/ | |
function myNumber(a) { | |
var resp = a; | |
return { | |
add: function(...args) { | |
for(i in args) { | |
resp += args[i]; | |
} | |
return this; | |
}, | |
subtract: function(...args) { | |
for(i in args) { | |
resp -= args[i]; | |
} | |
return this; | |
}, | |
value: function(){ | |
return resp; | |
} | |
} | |
} | |
myNumber(10).subtract(3).add(10,1,3).value(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment