Advertisement
Spocoman

Baking Competition

Oct 6th, 2023
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bakingCompetition(input) {
  2.     let bakers = Number(input[0]);
  3.     let index = 1, totalSweets = 0, totalSum = 0;
  4.  
  5.     for (let i = 0; i < bakers; i++) {
  6.         let baker = "";
  7.         let cookies = 0, cakes = 0, waffles = 0;
  8.  
  9.         while (true) {
  10.             let command = input[index++];
  11.             if (command == "Stop baking!") {
  12.                 break;
  13.             }
  14.  
  15.             if (baker == "") {
  16.                 baker = command;
  17.             } else {
  18.                 let sweet = command;
  19.                 let sweetCount = Number(input[index++]);
  20.                 if (sweet == "cookies") {
  21.                     cookies += sweetCount;
  22.                 } else if (sweet == "cakes") {
  23.                     cakes += sweetCount;
  24.                 } else {
  25.                     waffles += sweetCount;
  26.                 }
  27.                 totalSweets += sweetCount;
  28.             }
  29.         }
  30.         totalSum += 1.50 * cookies + 7.80 * cakes + 2.30 * waffles;
  31.         console.log(`${baker} baked ${cookies} cookies, ${cakes} cakes and ${waffles} waffles.`);
  32.     }
  33.  
  34.     console.log(`All bakery sold: ${totalSweets}\nTotal sum for charity: ${totalSum.toFixed(2)} lv.`);
  35.     return;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement