Reputation: 159
Write a program that predicts the approximate size of a population of organisms. Use the following data:
- Starting number of organisms: 2
- Average daily increase: 30%
- Number of days to multiply: 10
The program should display the following table of data:
Day Approiximate Population
1 2
2 2.6
3 3.38
4 4.39
5 5.71
6 7.42
7 9.65
8 12.54
9 16.31
10 21.20
My code is not outputting the same Approximate Population. Where did I go wrong? Here is my code:
var NumOfOrganisms = 2;
var DailyIncrease = .30;
var NumOfDays;
for(NumOfDays = 1; NumOfDays <= 10; NumOfDays++){
calculation(NumOfOrganisms, DailyIncrease, NumOfDays);
}
function calculation(organisms, increase, days){
var calculation = (organisms * increase) + days;
console.log("increase is " + calculation);
}
Upvotes: 5
Views: 115
Reputation: 43728
You do not take in consideration the evolving population.
var NumOfOrganisms = 2;
var DailyIncrease = .30;
var NumOfDays;
console.log('initial population', NumOfOrganisms);
for(NumOfDays = 2; NumOfDays <= 10; NumOfDays++) {
NumOfOrganisms = (NumOfOrganisms * DailyIncrease) + NumOfOrganisms;
console.log('increase is', NumOfOrganisms);
}
Upvotes: 1
Reputation: 135
shouldn't calculation equal something more like organisms+(organisms*increase)? And then if you keep a running total, you don't need to feed your function the number of days
Upvotes: 1