Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. class PointCard {
  2. constructor() {
  3. this.points = 0;
  4. this.fraction = 0;
  5. this.jpyPerPoint = 100;
  6. }
  7.  
  8. earn(jpy) {
  9. if (jpy <= 0) {
  10. throw RangeError(`jpy should be posivite. ${jpy} was given.`);
  11. }
  12. let pointsEarned = Math.floor(jpy / this.jpyPerPoint);
  13. let fractionEarned = jpy % this.jpyPerPoint;
  14. this.points += pointsEarned;
  15. this.fraction += fractionEarned;
  16. if (this.fraction >= this.jpyPerPoint) {
  17. this.points++;
  18. this.fraction -= this.jpyPerPoint;
  19. }
  20. }
  21.  
  22. use(pointsUsed) {
  23. if (this.points < pointsUsed) {
  24. throw RangeError(`pointsUsed was larger than current points. pointsUsed = ${pointsUsed}, current points = ${this.points}`);
  25. }
  26. this.points -= pointsUsed;
  27. }
  28. }
  29.  
  30. let p = new PointCard();
  31. p.earn(1580);
  32. console.log(p.points);
  33. console.log(p.fraction);
  34. p.earn(30);
  35. console.log(p.points);
  36. console.log(p.fraction);
  37. p.use(10);
  38. p.use(10);
  39. p.use(10);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement