Advertisement
bebo231312312321

Untitled

Apr 6th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function foo(arr) {
  2.     arr = arr.map(x => Number(x))
  3.     const days = arr.shift()
  4.     const budget = arr.shift()
  5.     const people = arr.shift()
  6.     const fuelPerKmPrice = arr.shift()
  7.     const foodPerPerson = arr.shift()
  8.     const roomPerPerson = arr.shift()
  9.     let hotelDiscount = 1
  10.     let expenses = 0
  11.  
  12.     function exceed(budget, expense) {
  13.         return expense > budget
  14.     }
  15.  
  16.     if (people > 10) hotelDiscount = 0.75
  17.  
  18.     expenses += foodPerPerson * people * days + roomPerPerson * people * days * hotelDiscount
  19.     for (let i = 0; i < days; i++) {
  20.         expenses += arr[i] * fuelPerKmPrice
  21.         if (exceed(budget, expenses))
  22.             return `Not enough money to continue the trip. You need ${(expenses - budget).toFixed(2)}$ more.`
  23.         if ((i + 1) % 3 === 0 || ((i + 1) % 5 === 0 && (i + 1) % 7 !== 0)) {
  24.             expenses += expenses * 0.4
  25.             if (exceed(budget, expenses))
  26.                 return `Not enough money to continue the trip. You need ${(expenses - budget).toFixed(2)}$ more.`
  27.         }
  28.         if ((i + 1) % 7 === 0) {
  29.             expenses -= expenses / people
  30.         }
  31.     }
  32.  
  33.     return `You have reached the destination. You have ${(budget - expenses).toFixed(2)}$ budget left.`
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement