Advertisement
kStoikow

cinemaVoucher.js

Jul 3rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function voucher(input) {
  2.     let voucherMoney = Number(input.shift());
  3.     let command = input.shift();
  4.     let ticketsBought = 0;
  5.     let foodBought = 0;
  6.  
  7.     while (command !== 'End' || voucherMoney > 0) {
  8.         let toBuy = (command);
  9.         let price = 0;
  10.  
  11.         if (command === 'End') {
  12.             break;
  13.  
  14.         }
  15.         if (toBuy.length > 8) {
  16.             for (let i = 0; i < 2; i++) {
  17.                 price += toBuy.charCodeAt(i);
  18.             }
  19.  
  20.             if (voucherMoney >= price) {
  21.                 ticketsBought++;
  22.                 voucherMoney -= price;
  23.  
  24.             } else {
  25.                 break;
  26.             }
  27.  
  28.         } else {
  29.             price = toBuy.charCodeAt(0);
  30.  
  31.             if (voucherMoney >= price) {
  32.                 foodBought++;
  33.                 voucherMoney -= price;
  34.  
  35.             } else {
  36.                 break;
  37.             }
  38.         }
  39.  
  40.         command = input.shift();
  41.     }
  42.     console.log(ticketsBought);
  43.     console.log(foodBought);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement