0

I am having problems displaying the results of function calculateGrowth. As shown below, I would like to find the results for each day of a 10 day period. I used a for loop for obvious reasons. But when I try to display results, all I get back is one result.

function calculateGrowth(){
    $days = 0;
    $growth = 10;
    for($days = 0; $days < 10; $days++){
      $totalGrowth = $growth * pow(2, $days/10);
    }
    return $totalGrowth;
}

Current Output

18.6606598307

Desired Output

Day Growth
1 - result
. - result
. - result
10

3 Answers 3

2
$totalGrowth = $growth * pow(2, $days/10);

Should be

$totalGrowth[] = $growth * pow(2, $days/10);
            ^^

So that it becomes an array and contains all the values that you add to it, rather than being a string which gets overwritten on every iteration of the loop.

Sign up to request clarification or add additional context in comments.

Comments

1

It sounds like what you're trying to get this:

function calculateGrowth() {    
    list($days, $growth, $table) = array(range(1, 10), 10, array());

    foreach ($days as $day) {

      $table[$day] = $growth * pow(2, $day/10);
    }

    return $table;
}

Comments

0

It is correct as your final loop is doing this

$totalGrowth = 10 * pow(2, 9/10)
$totalGrowth = 10*1.8660659830736;
$totalGrowth = 18.660659830736;

Edit : Removed last comment

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.