Advertisement
Spocoman

Beer Stock

Oct 11th, 2023
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function beerStock(input) {
  2.     let neededBeers = Number(input[0]);
  3.     let deliveredBeers = 0, index = 1;
  4.     let command;
  5.  
  6.     while ((command = input[index++]) != "Exam Over") {
  7.         var package = command.split(' ');
  8.         let amount = Number(package[0]);
  9.         let type = package[1];
  10.  
  11.         if (type == "sixpacks") {
  12.             amount *= 6;
  13.         } else if (type == "cases") {
  14.             amount *= 24;
  15.         }
  16.         deliveredBeers += amount;
  17.     }
  18.  
  19.     deliveredBeers -= parseInt(deliveredBeers / 100);
  20.     let diff = Math.abs(neededBeers - deliveredBeers);
  21.     let cases = parseInt(diff / 24);
  22.     let sixpacks = parseInt(diff % 24 / 6);
  23.     let beers = parseInt(diff % 6);
  24.  
  25.     if (deliveredBeers >= neededBeers) {
  26.         console.log(`Cheers! Beer left: ${cases} cases, ${sixpacks} sixpacks and ${beers} beers.`);
  27.     } else {
  28.         console.log(`Not enough beer. Beer needed: ${cases} cases, ${sixpacks} sixpacks and ${beers} beers.`);
  29.     }
  30.     return;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement