Liliana797979

class storage - fundamentals

Jul 3rd, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Storage {
  2.     constructor(capacity) {
  3.         this.capacity = capacity;
  4.         this.storage = [];
  5.     }
  6.  
  7.     get totalCost() {        
  8.         return this.storage.reduce((acc, el) => {return acc + el.price * el.quantity}, 0);
  9.     }
  10.  
  11.     addProduct(product) {
  12.         this.storage.push(product);
  13.         this.capacity -= product.quantity;
  14.         return;
  15.     }
  16.  
  17.     getProducts() {
  18.         let output = [];
  19.         this.storage.forEach(product => {
  20.             output.push(JSON.stringify(product));
  21.         })
  22.         return output.join('\n');
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment