Advertisement
Spocoman

01. Match Tickets

Dec 22nd, 2021 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function matchTickets(input) {
  2.     let budget = Number(input[0]);
  3.     let category = input[1];
  4.     let people = Number(input[2]);
  5.  
  6.     if (people <= 4) {
  7.         budget *= 0.25;
  8.     } else if (people <= 9) {
  9.         budget *= 0.4;
  10.     } else if (people <= 24) {
  11.         budget *= 0.5;
  12.     } else if (people < 50) {
  13.         budget *= 0.6;
  14.     } else {
  15.         budget *= 0.75;
  16.     }
  17.  
  18.     if (category === "VIP") {
  19.         budget -= 499.99 * people;
  20.     } else {
  21.         budget -= 249.99 * people;
  22.     }
  23.  
  24.     if (budget >= 0) {
  25.         console.log(`Yes! You have ${budget.toFixed(2)} leva left.`);
  26.     } else {
  27.         console.log(`Not enough money! You need ${Math.abs(budget).toFixed(2)} leva.`);
  28.     }
  29. }
  30.  
  31. Решение с тернарен оператор:
  32.  
  33. function matchTickets(input) {
  34.     let budget = Number(input[0]);
  35.     let category = input[1];
  36.     let people = Number(input[2]);
  37.  
  38.     budget *=
  39.         people <= 4 ? 0.25 :
  40.             people <= 9 ? 0.4 :
  41.                 people <= 24 ? 0.5 :
  42.                     people < 50 ? 0.6 : 0.75;
  43.  
  44.     budget -= (category === "VIP" ? 499.99 : 249.99) * people;
  45.  
  46.     console.log(budget >= 0 ? `Yes! You have ${budget.toFixed(2)} leva left.`
  47.         : `Not enough money! You need ${Math.abs(budget).toFixed(2)} leva.`);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement