Skip to content

Instantly share code, notes, and snippets.

@abhinavnigam2207
Created March 5, 2019 10:39
Show Gist options
  • Save abhinavnigam2207/8f9e8b5774c0b81a21f780fde9c5c0c0 to your computer and use it in GitHub Desktop.
Save abhinavnigam2207/8f9e8b5774c0b81a21f780fde9c5c0c0 to your computer and use it in GitHub Desktop.
Make the below code work (Asked in amazon interview)
/*
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