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

Nicolai Simonsen
Nicolai Simonsen
4,600 Points

Challenge Task1/Task2 not passing. Can't find error.

Hi,

I'm not quite sure why task 1 is not passing when checking work for task 2.

Maybe somebody can help identifying the problem.

$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',
);

Task 1 - Displaying 1-100

for ($i = 1; $i <= 100; $i++){      
       echo $i;
    }

Task 2 - Displaying values where key isset

for ($i = 1; $i <= 100; $i++){  
       if (isset($facts[$i])){          
          echo $i . " " . $facts[$i] . "<br/>\n";       
       } 
     }
John Roque Jorillo
John Roque Jorillo
13,117 Points

remove the , at the last index of the array $facts

$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' );

2 Answers

Hi Nicolai,

I assume you're showing task 1 and 2 individually but that you don't actually have 2 for loops when you're trying task 2.

For task 2 you still need to echo every number but optionally echo a fact after the number if there is one.

So you need to put your echo $i before your if condition and then inside your if condition you'll echo the fact.

Don't put in any extra formatting like that space or the line breaks unless specifically requested. It could throw off the output and not pass.

Nicolai Simonsen
Nicolai Simonsen
4,600 Points

Hi Jason,

Very kind of you to help me understand the problem. I've fixed it and it now works the way it was intended. I felt like the extra formatting helped me with what I was doing, but obviously I'll just remove it from now on, when submitting these challenges.

Thanks a bunch! Have a great weekend.

You're welcome.

Sometimes you have the freedom to format the output but often times the tester is looking for an exact match with the output.

On a real project you would definitely want to format the output nicely like you had with spaces and line breaks.

John Roque Jorillo
John Roque Jorillo
13,117 Points

remove the , at the last index of the array $facts $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' );

Nicolai Simonsen
Nicolai Simonsen
4,600 Points

Hi John,

Thanks for the input but I'm afraid it didn't do anything. What's the explanation for removing the (,) comma in the end of the array?

PHP does allow you to have a comma after the last element in an array.

The idea here is that if you need to add another element at the end at a later time, you won't have to remember to put the comma in because it's already there.