Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(data) {
- class Baked {
- constructor(cookies, waffles, cakes) {
- this.cookies = cookies;
- this.waffles = waffles;
- this.cakes = cakes;
- }
- getPrice() {
- return this.cookies * 1.5 + this.waffles * 2.3 + this.cakes * 7.8;
- }
- getCount() {
- return this.cookies + this.waffles + this.cakes;
- }
- }
- let names = {};
- let size = Number(data.shift());
- let count = 0;
- let bakesCount = 0;
- let finalPrice = 0;
- for (let i = 0; i < size; i += 1) {
- let index = data.indexOf(`Stop baking!`);
- let currentArr = data.splice(0, index);
- data.splice(0, 1);
- let name = currentArr.shift();
- if (!names[name]) {
- names[name] = new Baked(0, 0, 0);
- }
- while (currentArr.length > 0) {
- let baked = currentArr.shift();
- let qty = Number(currentArr.shift());
- names[name][baked] += qty;
- }
- }
- Object.entries(names).forEach(pair => {
- console.log(
- `${pair[0]} baked ${pair[1].cookies} cookies, ${pair[1].cakes} cakes and ${pair[1].waffles} waffles.`
- );
- bakesCount += pair[1].getCount();
- finalPrice += pair[1].getPrice();
- });
- console.log(`All bakery sold: ${bakesCount}`);
- console.log(`Total sum for charity: ${finalPrice.toFixed(2)} lv.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement