-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenough_water_for_how_many_days.js
36 lines (32 loc) · 1.27 KB
/
enough_water_for_how_many_days.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Description:
// Hello dweller,
//
// I'm the overseer of our vault, in which we all live.
//
// I make it short: We are out of water. The only question is when!
// Here is a list of all dwellers with their respective age int[] ageOfDweller.
// In our tank currently are int water liters of water.
// I want to know from you: How long will rich our supplies.
//
// Remember!
// Each dweller has a different water consumption.
// A dweller under 18 consumes 1 liter per day, everyone older than 50 needs 1.5 liters and the rest needs 2 liters per day.
// Each dweller must get its prescribed ration of water, every day!
// If not satisfied all dweller, then our days are numbered.
//
// Good luck! I'll bet on you!
//
//
// Note from Vault Technicians:
// Your program returns a positive integer. The residual water is not calculated.
// return -1; - If no dweller living inside of the Vault.
// SOLUTION:
function thirstyIn(water, ageOfDwellerArray) {
if (ageOfDwellerArray.length <=0) return -1
const waterDay = ageOfDwellerArray.map((age,index) =>{
if(age < 18) return 1
if(age >= 18 && age <= 50) return 2
if(age > 50) return 1.5
})
return Math.floor(water/waterDay.reduce((acc, el) =>acc+el,0))
}