Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PointCard {
- constructor() {
- this.points = 0;
- this.fraction = 0;
- this.jpyPerPoint = 100;
- }
- earn(jpy) {
- if (jpy <= 0) {
- throw RangeError(`jpy should be posivite. ${jpy} was given.`);
- }
- let pointsEarned = Math.floor(jpy / this.jpyPerPoint);
- let fractionEarned = jpy % this.jpyPerPoint;
- this.points += pointsEarned;
- this.fraction += fractionEarned;
- if (this.fraction >= this.jpyPerPoint) {
- this.points++;
- this.fraction -= this.jpyPerPoint;
- }
- }
- use(pointsUsed) {
- if (this.points < pointsUsed) {
- throw RangeError(`pointsUsed was larger than current points. pointsUsed = ${pointsUsed}, current points = ${this.points}`);
- }
- this.points -= pointsUsed;
- }
- }
- let p = new PointCard();
- p.earn(1580);
- console.log(p.points);
- console.log(p.fraction);
- p.earn(30);
- console.log(p.points);
- console.log(p.fraction);
- p.use(10);
- p.use(10);
- p.use(10);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement