Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

andrewjc
andrewjc
22,185 Points

Code Challenge: Adjusting Invalid Numbers

Hello,

I am stuck on this challenege... I'm just looking for a hint to make sure I'm on the right track - if anyone could help me out with that it would be greatly appreciated.

Thank you

4 Answers

Jake White
Jake White
41,721 Points

Thats weird. I just tested my code for a fourth time on the code challenge and the following code has worked every time.

if($number < 1){$count_less_than_one += 1;};
if($number >= 1 && $number <= 1000){$count_between_one_and_thousand += 1;};
if($number > 1000){$count_greater_than_thousand += 1;};

Are you putting in all of the if statements before submitting the error? Because all three have to be there, otherwise you'll get the error...

andrewjc
andrewjc
22,185 Points

I do have all three in when checking if it is correct - ill try it again maybe the challenge just needs to rest!

Ill try it again and let you know.

Thank you Jake and Randy for the help

andrewjc
andrewjc
22,185 Points

Hey, so I'm still getting the same error using this code:

if($number < 1){$count_less_than_one += 1;};

if($number >= 1 && $number <= 1000){$count_between_one_and_thousand += 1;};

if($number > 1000){$count_greater_than_thousand += 1;};

not sure what issue is :-(

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Hmmm ... those conditionals look correct to me. What does it say in the preview, how many numbers are less than one? Are they inside the foreach loop that was there at the beginning? Could you include all the code you have for the challenge?

andrewjc
andrewjc
22,185 Points

Here are the numbers in the preview: 1234 1412 1354

And here is the code

<?php

require_once('model.php');
$numbers = get_numbers();



    $count_less_than_one           = 0;
    $count_between_one_and_thousand = 0;
    $count_greater_than_thousand    = 0;

    foreach ($numbers as $number) {


        $count_less_than_one += 1;


        $count_between_one_and_thousand += 1;


        $count_greater_than_thousand += 1;

if($number < 1){$count_less_than_one += 1;}
if($number >= 1 && $number <= 1000){$count_between_one_and_thousand += 1;}
if($number > 1000){$count_greater_than_thousand += 1;}

    }



include('view.php');

?>
Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Ah, I see.

There are only a thousand numbers in the array total, so the counts for each category should be less than one thousand. Right now, you are counting every number in all three categories ... and then counting it a second time for the correct category.

Does that help?

andrewjc
andrewjc
22,185 Points

HAHAHA that’s a ridiculous mistake, I think I did everything possible except take out code...

Got it now thanks for all the help, much appreciated!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Right now, the foreach loop goes through all one thousand numbers. Some of them are less than 1, some of them are between 1-1000, and some of them are greater than 1000. We're looking to get a count of how many fit into each of those categories.

Right now, this line of code gets executed for every single number ...

$count_less_than_one += 1;

... making it look like we have one thousand numbers that are less than 1. However, this block of code should only be executed if the number under consideration in the foreach loop ($number) is less than 1. That will give us an accurate count.

Does that help?

andrewjc
andrewjc
22,185 Points

Hello Randy,

I am still stuck on this one I keep getting:

'You are including too many numbers in category (b). Please try again.'

I'm assuming this is one of the easier challenges I just can't seem to get it - I feel like Liza from the Simpson :-)..

Anyways here are my incomplete if statements I'm for sure lost on getting the count of numbers between 1-1000... Other than that I think I got the other two but I could be completely off..

if( 1 < $number ) {
    count($count_less_than_one);
}
if( $number ){
count(range(1, 1000));
}
if(1 > $number) {
count($count_greater_than_thousand);
}

If you could please let me know if I am completely lost on this one it be greatly appreciated.

Thank you for the help!

Jake White
Jake White
41,721 Points

You actually don't even need to use the count function for this challenge. And another hint that worked for me would be to write your if statement like this

if($number < 1){$count_less_than_one += 1;};

Hopefully that will help. I had to step away from this test for a couple of days and came back and figured it out.

andrewjc
andrewjc
22,185 Points

Hello Jake,

It did help a bit and I took some time away from the question but still can't get it... I think I'm on the right track with the statements, I believe they should say:

IF the variable $number is less than 1 THEN count the numbers less than 1 contained within $number.

IF the variable $number is greater than 1 AND less than 1000 THEN count the numbers in between 1, 1000

IF the variable $number is greater than 1000 THEN count the numbers greater than 1000

With that being said I believe I need to figure out what goes after the variable in the conditional area... Here is my current situation via block of code:

if($number < 1){$count_less_than_one ??????;}

if($number < 1000){$count_between_one_and_thousand ???????;}

if($number > 1000){$count_greater_than_thousand ???????;}

I'm pretty lost when it comes to the counting in between but I'm getting the error for section (B) which I believe is the less than 1 section.

If you could please let me know if I'm getting closer to the solution or if I’m still completely off it would be greatly appreciated.

Thanks again for your help!

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

You are definitely on the right track. What if I said it like this:

"I want to keep a count of numbers that are less than one. I'll start that count at zero. Then I'll loop through all the numbers one after the other. For each number, I'll look to see if the number is less than 1. If the number is less than one, then I'll increase that count of numbers by one. (Remember that this count started at zero, and each time we encounter a number less than one we'll add one to this count.)"

Does that help?

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

By the way, I meant to say that this conditional is correct:

if($number < 1) {

}

This correctly checks if the number is less than one. The next step, which I described above, is what do you do inside this conditional.

andrewjc
andrewjc
22,185 Points

Okay I think I'm getting there..

I need the IF statement to loop though the $number variable to count all the numbers less than 1 in the array.

So far I have this:

if ($number < 1){
        $count_less_than_one ++;
 } 

So from this I figured that sense $count_less_than_one is equal to zero so the ++ then returns $count_less_than_one, in increments $count_less_than_one by one starting from zero.

Obviously this statement doesn't work and I believe that I am missing code that loops through the numbers in the $number varible, but I could be completely off on the conditional statement.

I interpreted the above statement that because the if statement is checking for numbers less than one in the $number variable so it doesn’t stop at finding only one number less than one that it loops through finding all of them. From here it would take a count of all of the numbers starting from zero.

If that is the case would I use a while statement to create a loop? Or am I just completely off with the code I have here and my thought process.

Additionally, I either get 'You are including too many numbers in category (b). Please try again.' or You are not including enough numbers in category (b). Please try again.' I usually get 4 or 1234 as a result.

Jake White
Jake White
41,721 Points

You don't need to really adjust anything that has already been provided. All you need to do is add in the if statements.

if($number < 1){...};

if($number >= 1 && $number <= 1000){...};

if($number > 1000){...};

Just replace the ... with the code that has already been provided.

andrewjc
andrewjc
22,185 Points

Like so?

if($number < 1){$count_less_than_one += 1;};

if($number >= 1 && $number <= 1000){$count_between_one_and_thousand += 1;};

if($number > 1000){$count_greater_than_thousand += 1;};

I have tried this and it still gives me the error message "You are including too many numbers in category (b). Please try again."

What am I missing :-s?

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Hey Andrew,

You don't need semi-colons after the closing curly brackets in a conditional like this. Instead of this ...

if($number < 1){$count_less_than_one += 1;};

... you should have this ...

if($number < 1){$count_less_than_one += 1;}

Does that help?

andrewjc
andrewjc
22,185 Points

Hey Randy,

I did try it with without the semi-colons after the curly brackets and I'm still getting the same error.

"Bummer! You are including too many numbers in category (b). Please try again."

Michael Walker
Michael Walker
12,466 Points

I'm still a novice on this programming stuff, but I didn't have any trouble with this one. This entire problem can be handled with one if, else if, else, statement using Randy's last hint. Just make sure the semi-colons are inside the curly brackets. :-)