Advertisement
Spocoman

08. Fuel Tank - Part 2

Dec 18th, 2021 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fuelTank(input) {
  2.     let fuel = input[0]
  3.     let liters = Number(input[1])
  4.     let cart = input[2]
  5.     let literPrice = 0
  6.  
  7.     if (fuel == 'Gas') {
  8.         if (cart == 'Yes') {
  9.             literPrice = 0.85;
  10.         } else {
  11.             literPrice = 0.93;
  12.         }
  13.     } else if (fuel == 'Diesel') {
  14.         if (cart == 'Yes') {
  15.             literPrice = 2.21;
  16.         } else {
  17.             literPrice = 2.33;
  18.         }
  19.     } else if (fuel == 'Gasoline') {
  20.         if (cart == 'Yes') {
  21.             literPrice = 2.04;
  22.         } else {
  23.             literPrice = 2.22;
  24.         }
  25.     }
  26.  
  27.     if (liters >= 20 && liters <= 25) {
  28.         literPrice *= 0.92;
  29.     } else if (liters > 25) {
  30.         literPrice *= 0.9;
  31.     }
  32.  
  33.     console.log(`${(literPrice * liters).toFixed(2)} lv.`)
  34. }
  35.  
  36. Второ решение с тернарен оператор:
  37.  
  38. function fuelTank(input) {
  39.     let fuel = input[0]
  40.     let liters = Number(input[1])
  41.     let cart = input[2]
  42.  
  43.     let literPrice =
  44.         fuel == 'Gas' ? (cart == 'Yes' ? 0.85 : 0.93) :
  45.             fuel == 'Diesel' ? (cart == 'Yes' ? 2.21 : 2.33) :
  46.                 fuel == 'Gasoline' ? (cart == 'Yes' ? 2.04 : 2.22) : 0;
  47.  
  48.     literPrice *= liters >= 20 && liters <= 25 ? 0.92 : liters > 25 ? 0.9 : 1;
  49.  
  50.     console.log(`${(literPrice * liters).toFixed(2)} lv.`)
  51. }
  52.  
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement