Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function beerStock(input) {
- let neededBeers = Number(input[0]);
- let deliveredBeers = 0, index = 1;
- let command;
- while ((command = input[index++]) != "Exam Over") {
- var package = command.split(' ');
- let amount = Number(package[0]);
- let type = package[1];
- if (type == "sixpacks") {
- amount *= 6;
- } else if (type == "cases") {
- amount *= 24;
- }
- deliveredBeers += amount;
- }
- deliveredBeers -= parseInt(deliveredBeers / 100);
- let diff = Math.abs(neededBeers - deliveredBeers);
- let cases = parseInt(diff / 24);
- let sixpacks = parseInt(diff % 24 / 6);
- let beers = parseInt(diff % 6);
- if (deliveredBeers >= neededBeers) {
- console.log(`Cheers! Beer left: ${cases} cases, ${sixpacks} sixpacks and ${beers} beers.`);
- } else {
- console.log(`Not enough beer. Beer needed: ${cases} cases, ${sixpacks} sixpacks and ${beers} beers.`);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement