age*_*123 5 javascript function range
编写一个程序,预测一群生物的大致尺寸.使用以下数据:
- 起始生物数量:2
- 平均每日增幅:30%
- 乘以的天数:10
该程序应显示以下数据表:
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
Run Code Online (Sandbox Code Playgroud)
我的代码没有输出相同的近似人口.我哪里做错了?这是我的代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
您没有考虑不断变化的人口。
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |