Last active
December 3, 2018 09:26
-
-
Save segunolalive/582b2bf2d00a438b2beeec9bb33272b6 to your computer and use it in GitHub Desktop.
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
| class Arithmetic { | |
| constructor() { | |
| this.value = 0; | |
| } | |
| get val() { | |
| return this.value; | |
| } | |
| sum(...args) { | |
| this.value = args.reduce((sum, current) => sum + current, 0); | |
| return this; | |
| } | |
| add(value) { | |
| this.value += value; | |
| return this; | |
| } | |
| subtract(value) { | |
| this.value -= value; | |
| return this; | |
| } | |
| average(...args) { | |
| this.value = args.length | |
| ? (this.sum(...args).value) / args.length | |
| : undefined; | |
| return this; | |
| } | |
| } | |
| a = new Arithmetic() | |
| a.sum(1, 3, 6) // => { value: 10 } | |
| .subtract(3) // => { value: 7 } | |
| .add(4) // => { value: 11 } | |
| .val // => 11 | |
| // Console Output | |
| // 11 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment