const calculator = (() => { const numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']; class Unit { constructor(num, type) { return numbers.reduce((acc, n, i) => ({ ...acc, get [n]() { switch (type) { case 'PLUS': return new Num(num + (i + 1)); case 'MINUS': return new Num(num - (i + 1)); case 'MULTIPLY': return new Num(num * (i + 1)); case 'DIVISION': return new Num(num / (i + 1)); } } }), {}); } } class Num { constructor(num) { this.num = num; } get equals() { return this.num; } get plus() { return new Unit(this.num, 'PLUS'); } get minus() { return new Unit(this.num, 'MINUS'); } get multipleTo() { return new Unit(this.num, 'MULTIPLY'); } get dividedBy() { return new Unit(this.num, 'DIVISION'); } } return new Unit(0, 'PLUS'); })();
Not how I did it, but works equally well! Loving it 👏
How do you make it without writing like this? :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hong Kong No IT (香港創科局高官二世祖圍威喂)
Not how I did it, but works equally well! Loving it 👏
How do you make it without writing like this? :)