SecondArray.js 808 B

123456789101112131415161718192021222324252627282930313233
  1. export default class SecondArray {
  2. constructor() {
  3. this.circularData = []
  4. }
  5. add(e) {
  6. this.circularData.push(e)
  7. }
  8. getAvg() {
  9. let e = 0;
  10. for (let i = 0; i < this.circularData.length; i++) e += this.circularData[i];
  11. return {
  12. sum: e,
  13. avg: e / this.circularData.length || 0
  14. }
  15. }
  16. getMax() {
  17. let e = 0;
  18. for (let i = 0; i < this.circularData.length; i++) e < this.circularData[i] && (e = this.circularData[i]);
  19. return e || 0
  20. }
  21. clear() {
  22. this.circularData = []
  23. }
  24. getStat() {
  25. const e = this.getAvg(),
  26. i = {
  27. sum: e.sum,
  28. avg: e.avg,
  29. max: this.getMax()
  30. };
  31. return this.clear(), i
  32. }
  33. }