RRusev77

07. Movie tickets

Apr 25th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let movieName = input.shift();
  3.     let ticketStudentCount = 0;
  4.     let ticketStandardCount = 0;
  5.     let ticketKidsCount = 0;
  6.     let takenPlaces;
  7.     let ticketsBought = 0;
  8.     let isFinished = false;
  9.  
  10.     while(movieName != 'Finish') {
  11.         // restart all vars
  12.         takenPlaces = 0;
  13.         let freePlaces = Number(input.shift());
  14.         let ticketType = input.shift();
  15.         // loop
  16.         while(ticketType != 'End') {
  17.            
  18.             if(ticketType == 'Finish') {
  19.                 isFinished = true;
  20.                 break;
  21.             }
  22.            
  23.             if(ticketType == 'standard') {
  24.                 ticketStandardCount++;
  25.             } else if (ticketType == 'student') {
  26.                 ticketStudentCount++;
  27.             } else {
  28.                 ticketKidsCount++;
  29.             }
  30.  
  31.             takenPlaces++;
  32.             ticketsBought++;
  33.             ticketType = input.shift();
  34.         }
  35.         // result
  36.         let percentTakenPlaces = (takenPlaces / freePlaces) * 100;
  37.         console.log(`${movieName} - ${percentTakenPlaces.toFixed(2)}% full.`);
  38.         movieName = input.shift();
  39.         if(isFinished) {
  40.             break;
  41.         }
  42.     }
  43.  
  44.     let percentKids = (ticketKidsCount / ticketsBought) * 100;
  45.     let percentStandard = (ticketStandardCount / ticketsBought) * 100;
  46.     let percentStudent = (ticketStudentCount / ticketsBought) * 100;
  47.  
  48.     console.log(`Total tickets: ${ticketsBought}`);
  49.     console.log(`${percentStudent.toFixed(2)}% student tickets.`);
  50.     console.log(`${percentStandard.toFixed(2)}% standard tickets.`);
  51.     console.log(`${percentKids.toFixed(2)}% kids tickets.`);
  52. }
Add Comment
Please, Sign In to add comment