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 trialAlfredo Prince
6,175 PointsMy return should be an array. isn't that what I'm doing?
I'm certain i did something else wrong here. A little guidance would be helpful.
<?php
// add code below this comment
class Subdivision
{
public $houses = array();
public function filterHouseColor($color)
{
foreach($houses as $house){
if($house->$roof_color || $house->$wall_color == $this->$color){
$this->$houses[] = $house;
}
return $houses['house'];
}
}
}
?>
Jason Anello
Courses Plus Student 94,610 PointsTagging Alena Holligan
I think there might be a problem with the challenge test on this one. It doesn't seem to be checking the values of the elements in the returned array.
2 Answers
Eric Drake
6,339 PointsAlfredo,
I think you and I covered this problem in another conversation. This is a confusing challenge. Here are a few details that the challenge doesn't give you that you need to know:
- The houses property is an array of House objects.
- The House class has three public member variables: lot, roof_color, and wall_color.
You are supposed to loop through the houses array and create a separate array that only contains the House objects that have a roof_color or wall_color equal to the color passed to the filterHouseColor method. The result looks something like this:
public function filterHouseColor($color) {
$matches = array();
for ($i=0;$i<count($this->houses);$i++){
if ($this->houses[$i]->roof_color == $color || $this->houses[$i]->wall_color == $color) {
$matches[] = array('lot' => $this->houses[$i]->lot); } } return $matches;
}
}
return $matches;
}
Jason Anello
Courses Plus Student 94,610 PointsHi Eric,
The code could be a little easier with a foreach loop.
I think you're returning the wrong array structure here. You're creating a multi-dimensional array with the inner arrays being associative arrays.
It's supposed to be a single array of lot values.
$matches[] = $this->houses[$i]->lot;
That would add each lot value to the array.
Alena Holligan
Treehouse TeacherI updated the code challenge to be more specific and also return more hints to point you in the right direction. I'd appreciate it if people would check it out and give me any feedback on how else it could be improved. Alfredo Prince , Jason Anello , Eric Drake , Corina Meyer
Jason Anello
Courses Plus Student 94,610 PointsHi Alena,
Testing seems to be good now. I can't find any incorrect code that passes.
The instructions have changed for task 3. Is it ok that the existing answers for this question will no longer match up?
Alena Holligan
Treehouse TeacherThanks @jason! It's ok if they don't match, thanks for calling it out here. STEP 3 HAS BEEN CHANGED
Corina Meyer
9,990 PointsCorina Meyer
9,990 PointsThere are several issues in your code that could have caused the challenge to fail.
i hope this helps.