Advertisement
Liliana797979

viarno reshenie cinema tickets1

Feb 21st, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. // 07. Cinema Tickets
  3.  
  4. function solve(arg) {
  5.     let idx = 0;
  6.     let command = arg[idx++];  // idx++ (!?)
  7.     let ticketsTotalSold = 0;
  8.     let ticketsStudentSold = 0;
  9.     let ticketsStandardSold = 0;
  10.     let ticketsKidsSold = 0;
  11.  
  12.     while (command !== "Finish") {
  13.         let movieName = command;
  14.         let freeSeats = Number(arg[idx++]);  // idx++ (!?)
  15.         let ticketType = arg[idx++];  // idx++ (!?)
  16.         let ticketsCurrentSold = 0;
  17.         while (ticketType !== "End") {
  18.             switch (ticketType) {
  19.                 case "standard":
  20.                     ticketsStandardSold++;
  21.                     break;
  22.                 case "student":
  23.                     ticketsStudentSold++;
  24.                     break;
  25.                 case "kid":
  26.                     ticketsKidsSold++;
  27.                     break;
  28.             }
  29.             ticketsTotalSold++;  // OUTSIDE the SWITCH (!?)
  30.             ticketsCurrentSold++;
  31.             if (freeSeats <= ticketsCurrentSold) {  // 2nd condition to finish the inside cycle (!?)
  32.                 break;
  33.             }
  34.             ticketType = arg[idx++];
  35.         }
  36.         console.log(`${movieName} - ${(ticketsCurrentSold / freeSeats * 100).toFixed(2)}% full.`);
  37.         command = arg[idx];
  38.         idx++;
  39.     }
  40.     console.log(`Total tickets: ${ticketsTotalSold}`);
  41.     console.log(`${(ticketsStudentSold / ticketsTotalSold * 100).toFixed(2)}% student tickets.`);
  42.     console.log(`${(ticketsStandardSold / ticketsTotalSold * 100).toFixed(2)}% standard tickets.`);
  43.     console.log(`${(ticketsKidsSold / ticketsTotalSold * 100).toFixed(2)}% kids tickets.`);
  44. }
  45.  
  46. solve(["Taxi", "10", "standard", "kid", "student", "student", "standard", "standard", "End",
  47. "Scary Movie", "6", "student", "student", "student", "student", "student", "student",
  48. "Finish"]) ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement