ALSAVOV

Untitled

Nov 16th, 2023
228
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 vaucherValue = Number(input[index]);
  4.     index++;
  5.     let command = input[index];
  6.     let filmCounter = 0;
  7.     let purchaseCounter = 0;
  8.  
  9.     while (command !== "End") {
  10.         let purchase = command;
  11.         let filmPrice = 0;
  12.         let purchasePrice = 0;
  13.  
  14.         let firstChar = purchase[0];
  15.         let secondChar = purchase[1];
  16.  
  17.         if (purchase.length > 8) {
  18.             filmPrice =
  19.                 Number(firstChar.charCodeAt()) +
  20.                 Number(secondChar.charCodeAt());
  21.  
  22.             if (vaucherValue - filmPrice < 0) {
  23.                 break;
  24.             }
  25.  
  26.             vaucherValue -= filmPrice;
  27.             filmCounter++;
  28.         } else {
  29.             purchasePrice = Number(firstChar.charCodeAt());
  30.  
  31.             if (vaucherValue - purchasePrice < 0) {
  32.                 break;
  33.             }
  34.  
  35.             vaucherValue -= purchasePrice;
  36.             purchaseCounter++;
  37.         }
  38.  
  39.         index++;
  40.         command = input[index];
  41.     }
  42.     console.log(filmCounter);
  43.     console.log(purchaseCounter);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment