Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function springVacation(input) {
  2.  
  3.   let days = Number(input.shift());
  4.   let budget = Number(input.shift());
  5.   let peopleCount = Number(input.shift());
  6.   let fuelPerKm = Number(input.shift());
  7.   let foodPerPerson = Number(input.shift());
  8.   let hotelPerPerson = Number(input.shift());
  9.  
  10.   let distanceCost = 0;
  11.   //let traveledDistance = Number(input.shift()); // направо във for цикъла се чете
  12.   let currentExpenses = 0;
  13.  
  14.   let isBudgetEnough = true; // накрая за принтирането
  15.  
  16.   let foodForAll = peopleCount * foodPerPerson * days;
  17.   let hotelPriceForAll = peopleCount * hotelPerPerson * days;
  18.  
  19.  
  20.   if (peopleCount > 10) {
  21.     hotelPriceForAll -= hotelPriceForAll * 0.25;
  22.   }
  23.   currentExpenses = hotelPriceForAll + foodForAll;
  24.  
  25.   for (let day = 1; day <= days; day++) { // <= до последния ден включително
  26.  
  27.     let traveledDistance = Number(input.shift());
  28.     distanceCost = fuelPerKm * traveledDistance;
  29.     currentExpenses += distanceCost;
  30.  
  31.     if (day % 3 === 0 || day % 5 === 0) {
  32.       currentExpenses += currentExpenses * 0.40;
  33.     }
  34.  
  35.     if (day % 7 === 0) {
  36.       currentExpenses -= currentExpenses / peopleCount; // намаляват се разходите
  37.     }
  38.  
  39.     if (currentExpenses > budget) { // проверка накрая на цикъла защото може най-последния ден да надхвърли бюджета
  40.       console.log(`Not enough money to continue the trip. You need ${(currentExpenses - budget).toFixed(2)}$ more.`);
  41.       isBudgetEnough = false;
  42.       break;
  43.     }
  44.  
  45.   }
  46.  
  47.   if (isBudgetEnough) {
  48.     console.log(`You have reached the destination. You have ${(budget - currentExpenses).toFixed(2)}$ budget left.`);
  49.   }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement