gpt4 book ai didi

javascript - 我的 javascript 输出与预期输出不匹配。我不知道我哪里错了

转载 作者:搜寻专家 更新时间:2023-11-01 04:30:42 25 4
gpt4 key购买 nike

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

我的代码没有输出相同的近似人口。我哪里做错了?这是我的代码:

    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);
}

最佳答案

您没有考虑不断变化的人口。

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);
}

关于javascript - 我的 javascript 输出与预期输出不匹配。我不知道我哪里错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32461216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com