Skip to content

Instantly share code, notes, and snippets.

@segunolalive
Last active December 3, 2018 09:25
Show Gist options
  • Select an option

  • Save segunolalive/093eabf5b1ab2bc64328c9fac16ef420 to your computer and use it in GitHub Desktop.

Select an option

Save segunolalive/093eabf5b1ab2bc64328c9fac16ef420 to your computer and use it in GitHub Desktop.
class Arithmetic {
constructor() {
this.value = 0;
}
sum(...args) {
this.value = args.reduce((sum, current) => sum + current, 0);
return this;
}
add(value) {
this.value = this.value + value;
return this;
}
subtract(value) {
this.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 }
.value // => 11
// Console Output
// 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment