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
Andrew McCombs
4,309 PointsAdjusting Invalid Numbers
The $numbers array in the code below contains one thousand numbers. I need to count how many of these numbers fit into each of the following categories: (a) how many of them are between 1 and 1000 inclusive, (b) how many of them are less than 1, and (c) how many of them are greater than 1000. I have created a foreach loop to look at each number one after the other, but right now it’s treating every number like it belongs in all three categories. Modify the code below so that the counts are correct. http://teamtreehouse.com/library/adjusting-invalid-numbers
What did I do wrong
<?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) {
if($number >= 1){
$count_less_than_one ++ 1;
}
if(1 =< $number <= 1000){
$count_between_one_and_thousand ++ 1;
}
if($number >= 1000){
$count_greater_than_thousand ++ 1;
}
}
include('view.php');
?>
5 Answers
Robert Walker
17,146 PointsThis will pass it but you should look at it a little more and understand where you went wrong
foreach ($numbers as $number) {
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;
}
}
http://www.mathsisfun.com/equal-less-greater.html
I use this all the time as I still get confused on which to use and what not to use, ill get it one day but till then this is bookmarked for me.
David Omar
5,676 Pointsyou second if statement is invalid, you need to use the logical operator &&
<?php
if($number >= 1 && $number <= 1000)
?>
Andrew McCombs
4,309 Pointsit said try again..
<?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) {
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');
?>
David Omar
5,676 Pointsyou are using the increment operator wrong, you could either use
<?php
$count++;
//or
$count += 1;
//both add one to $couunt
?>
Andrew McCombs
4,309 PointsBummer! You are including too many numbers in category (b). Please try again.
Im about to just give up on this one
<?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) {
if($number >= 1){
$count_less_than_one ++;
}
if($number >= 1 && $number <= 1000){
$count_between_one_and_thousand ++;
}
if($number >= 1000){
$count_greater_than_thousand ++;
}
}
include('view.php');
?>
Andrew McCombs
4,309 PointsIf i do +=1 it will say the same Bummer! You are including too many numbers in category (b). Please try again.
David Omar
5,676 PointsOk i fixed it. your first if statement is >= 1 when it needs to be < 1. Your last if statement needs to be > 1000 and ++ has to be next to your variable
<?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) {
if($number < 1){
$count_less_than_one++;
}
if($number >= 1 && $number <= 1000){
$count_between_one_and_thousand++;
}
if($number > 1000){
$count_greater_than_thousand++;
}
}
include('view.php');
?>
Kyle Germaine
8,174 Points $count_less_than_one = 0;
$count_between_one_and_thousand = 0;
$count_greater_than_thousand = 0;
foreach ($numbers as $number) {
if ( $number < 1 ) {
$count_less_than_one++;
}
if ( $number >= 1 && $number <= 1000 ) {
$count_between_one_and_thousand++;
}
if ( $number > 1000){
$count_greater_than_thousand++;
}
}
You have a few problems here...
1) You need to add 2 comparisons to the second conditional (if statement). You need to compare the values independently and will get true if both pass.
2) Unless you wrote down the "logic" above incorrectly, you don't need the <= to check if less than 1 because this will pass if the number is 1. Same goes for the >= 1000, you don't need the =.
3) When you are incrementing the values ( $value ++ 1; ), you need to remove the 1. The ++ automatically adds 1 to the value and is a short form for ( $value = $value + 1; ), which would also work. Or ( $value += 1; ), which will increment be any value you enter not just 1.
Hope this helps, ~ Kyle
Emilio Liriano
11,090 PointsThis is how I did it.
<?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) {
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');
?>
Andrew McCombs
4,309 PointsAndrew McCombs
4,309 PointsIT WORKED^^^^^^^^^^^^^^^^^^^^