Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My javascript output does not match the expected output. I don't know where I went wrong

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);
    }
like image 358
agentmg123 Avatar asked Oct 30 '22 19:10

agentmg123


1 Answers

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);
}
like image 108
plalx Avatar answered Nov 03 '22 20:11

plalx