Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/makeCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,49 @@
* @return {object}
*/
function makeCalculator() {
// write code here
let result = 0;

return {
get result() {
return result;
},

add(number) {
result += number;

return this;
},

subtract(number) {
result -= number;

return this;
},

divide(number) {
result /= number;

return this;
},

multiply(number) {
result *= number;

return this;
},

operate(callback, arg) {
callback(arg);
Comment on lines +38 to +39

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation callback(arg); in operate does not guarantee that this inside methods like add refers to the calculator object when they are passed as callbacks. This violates the requirement that add, subtract, multiply, divide are passed as callbacks to operate and that chaining via this works. Consider invoking the callback with the calculator as its context, for example using callback.call(this, arg).


return this;
},

reset() {
result = 0;

return this;
},
};
}

module.exports = makeCalculator;
Loading