Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Arrays and Control Structures PHP Loops For Looping

What am i doing wrong, Task not passing

<?php $facts = array( 57 => ' on Heinz ketchup bottles represents the number of varieties of pickles the company once had.', 2 => ' is the approximate hours a day Giraffes sleeps', 18 => ' is the average hours a Python sleeps per day', 10 => ' per cent of the world is left-handed.', 11 => ' Empire State Buildings, stacked one on top of the other, would be required to measure the Gulf of Mexico at its deepest point.', 98 => '% of the atoms in your body are replaced every year', 69 => ' is the largest number of recorded children born to one woman', ); //add your loop below this line $num = 101; for($i = 0; ++$i < $num;){ echo $i . "\n"; isset($i === count($facts[0])){ echo $facts[$i] . "\n"; } };

index.php
<?php
$facts = array(
    57 => ' on Heinz ketchup bottles represents the number of varieties of pickles the company once had.',
    2 => ' is the approximate hours a day Giraffes sleeps',
    18 => ' is the average hours a Python sleeps per day',
    10 => ' per cent of the world is left-handed.',
    11 => ' Empire State Buildings, stacked one on top of the other, would be required to measure the Gulf of Mexico at its deepest point.',
    98 => '% of the atoms in your body are replaced every year',
    69 => ' is the largest number of recorded children born to one woman',
);
//add your loop below this line
$num = 101;
for($i = 0; ++$i < $num;){
  echo $i . "\n";
  isset($i === count($facts[0])){
    echo $facts[$i] . "\n";
  }
};

3 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Review your for loop syntax - do you start with the correct initialization, does your condition stop the loop at the correct time and does your final-expression do the right thing and the end of each iteration?.

Here's the syntax from MDN

for ([initialization]; [condition]; [final-expression]) statement

Task one is passed, i only get stock in task two, how am i suppose to use the isset together with the for loop

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Use the isset() as part of a conditional to know if the number (index) has a value in the $facts array and when it does print out that text.

for($i = 0; ++$i <= $num, isset(count($facts));){ echo . $i $facts[] . "\n"; }

Task one no longer passing..

please can you just help me with the code, I will get to understand better, please

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

OK, but debugging your code is the fun part :)

Here's how I would do it - let me know if you have any questions:

<?php
$facts = array(
    57 => ' on Heinz ketchup bottles represents the number of varieties of pickles the company once had.',
    2 => ' is the approximate hours a day Giraffes sleeps',
    18 => ' is the average hours a Python sleeps per day',
    10 => ' per cent of the world is left-handed.',
    11 => ' Empire State Buildings, stacked one on top of the other, would be required to measure the Gulf of Mexico at its deepest point.',
    98 => '% of the atoms in your body are replaced every year',
    69 => ' is the largest number of recorded children born to one woman',
);
//add your loop below this line
$num = 100;
for($i = 1; $i <= $num; $i++){
    echo $i;
    if (isset($facts[$i])) {
        echo $facts[$i];
    }
};

And it produces the following output - I skipped the newline since I don't think the challenge said anything about line breaks:

12 is the approximate hours a day Giraffes sleeps345678910 per cent of the world is left-handed.11 Empire State Buildings, stacked one on top of the other, would be required to measure the Gulf of Mexico at its deepest point.12131415161718 is the average hours a Python sleeps per day192021222324252627282930313233343536373839404142434445464748495051525354555657 on Heinz ketchup bottles represents the number of varieties of pickles the company once had.585960616263646566676869 is the largest number of recorded children born to one woman7071727374757677787980818283848586878889909192939495969798% of the atoms in your body are replaced every year99100