-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgrowth-of-a-population.js
More file actions
33 lines (25 loc) · 874 Bytes
/
growth-of-a-population.js
File metadata and controls
33 lines (25 loc) · 874 Bytes
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
// 1000 + 1000 * 0.02 + 50 // formula
function nbYear(p0, percent, aug, p) {
// a place to store the number of years, start at 0
let totalYears = 0;
const getNextPopulation = current => current + (current * percent/100) + aug;
// while population is less than or equal to p
while(p0 < p) {
// calculate population with formula
p0 = getNextPopulation(p0);
// increase the year counter
totalYears++;
}
return totalYears;
}
function nbYear(p0, percent, aug, p) {
const getNextPopulation = current => current + (current * percent / 100) + aug;
if (p0 < p) {
const nextPopulation = getNextPopulation(p0);
return 1 + nbYear(nextPopulation, percent, aug, p);
}
return 0;
}
console.log(nbYear(1500, 5, 100, 5000), 15);
console.log(nbYear(1500000, 2.5, 10000, 2000000), 10);
console.log(nbYear(1500000, 0.25, 1000, 2000000), 94);