Advertisement
maymunskoa

06. Baking Competition (PB Exam 270719)

Mar 26th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(data) {
  2.   class Baked {
  3.     constructor(cookies, waffles, cakes) {
  4.       this.cookies = cookies;
  5.       this.waffles = waffles;
  6.       this.cakes = cakes;
  7.     }
  8.     getPrice() {
  9.       return this.cookies * 1.5 + this.waffles * 2.3 + this.cakes * 7.8;
  10.     }
  11.     getCount() {
  12.       return this.cookies + this.waffles + this.cakes;
  13.     }
  14.   }
  15.  
  16.   let names = {};
  17.   let size = Number(data.shift());
  18.   let count = 0;
  19.   let bakesCount = 0;
  20.   let finalPrice = 0;
  21.  
  22. for (let i = 0; i < size; i += 1) {
  23.     let index = data.indexOf(`Stop baking!`);
  24.     let currentArr = data.splice(0, index);
  25.     data.splice(0, 1);
  26.     let name = currentArr.shift();
  27.     if (!names[name]) {
  28.       names[name] = new Baked(0, 0, 0);
  29.     }
  30.     while (currentArr.length > 0) {
  31.       let baked = currentArr.shift();
  32.       let qty = Number(currentArr.shift());
  33.       names[name][baked] += qty;
  34.     }
  35.   }
  36.  
  37.   Object.entries(names).forEach(pair => {
  38.     console.log(
  39.       `${pair[0]} baked ${pair[1].cookies} cookies, ${pair[1].cakes} cakes and ${pair[1].waffles} waffles.`
  40.     );
  41.     bakesCount += pair[1].getCount();
  42.     finalPrice += pair[1].getPrice();
  43.   });
  44.   console.log(`All bakery sold: ${bakesCount}`);
  45.   console.log(`Total sum for charity: ${finalPrice.toFixed(2)} lv.`);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement