-Enigmos-

cinemaVoucher.js

Nov 18th, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cinemaVoucher(input) {
  2.     let index = 0;
  3.     let voucherPrice = Number(input[index]);
  4.     index++;
  5.     let command = input[index];
  6.     index++;
  7.     let ticketCounter = 0;
  8.     let purchaseCounter = 0;
  9.  
  10.     while (command !== "End") {
  11.         if (command.length > 8) {
  12.             let ticketPrice = command.charCodeAt(0) + command.charCodeAt(1);
  13.             if ((voucherPrice - ticketPrice) < 0) {
  14.                 break;
  15.             } else {
  16.                 ticketCounter++;
  17.                 voucherPrice -= ticketPrice;
  18.             }
  19.         } else if (command.length <= 8) {
  20.             let purchasePrice = command.charCodeAt(0);
  21.             if ((voucherPrice - purchasePrice) < 0) {
  22.                 break;
  23.             } else {
  24.                 purchaseCounter++;
  25.                 voucherPrice -= purchasePrice;
  26.             }
  27.         }
  28.  
  29.         command = input[index];
  30.         index++;
  31.     }
  32.     console.log(ticketCounter);
  33.     console.log(purchaseCounter);
  34. }
  35.  
  36. cinemaVoucher(["300", "Captain Marvel", "popcorn", "Pepsi"]);
  37. cinemaVoucher(["1500", "Avengers: End Game", "Bohemian Rhapsody", "Deadpool 2", "End"]);
Advertisement
Add Comment
Please, Sign In to add comment