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 Object-Oriented PHP Basics Building a Collection Collection Objects

Adam Duffield
Adam Duffield
30,494 Points

Return an array containing the public lot parameter? Challenge 3 of 3 in Object Orientated Basics

Task is...

Return an array containing the public lot parameter of each house object whose public roof_color OR wall_color match the passed color parameter.

Current code is... ( Bare in mind all but the "return" are part of task 1 and 2 ).

// add code below this comment
class Subdivision {
 public $houses = array(); 

  public function filterHouseColor($color) {
    return;
  }
}


?>

I'm no stranger to Object Orientated Programming or Php, just looking to complete some courses to learn a few extra pockets of knowledge but this task makes no sense to me. What is the public lot parameter? Am I supposed to be given a house class to play with? The task seems far too unclear and I have no idea how to go about completing it.

Any guidance would be really appreciated.

Thanks,

Adam

index.php
<?php

// add code below this comment
class Subdivision {
 public $houses = array(); 

  public function filterHouseColor($color) {
    return;
  }
}


?>
Nurrokhman Hidayat
Nurrokhman Hidayat
12,705 Points
<?php
class Subdivision {
  public $houses = [];

  public function filterHouseColor($color) {
    $lots = [];  //create array

    foreach ($this->houses as $house) {  //loop through array

      if ($house->roof_color === $color || $house->wall_color === $color) {
        $lots[] = $house->lot; 
      }
    }

    return $lots;  
  }
}

?>

5 Answers

Joel Bardsley
Joel Bardsley
31,246 Points

EDiT: It's a shame someone's just given you the answer as I've been writing this, but I'll post it anyway in case it comes in handy for you or anyone else trying the challenge themselves.

Hi Adam,

This task has proved to be a tricky one among the community, it does kinda throw you in at the deep end after the first two gentler tasks!

I think the public lot is a property of the House object rather than a parameter, so the wording of the question could be improved. If I'm wrong, hopefully someone else can explain why. I'll paste the question below and give you a few hints to get you started:

Return an array containing the public lot parameter of each house object whose public roof_color OR wall_color match the passed color parameter.

<?php

// add code below this comment
class Subdivision {
 public $houses = array(); // array of House objects

  public function filterHouseColor($color) {
    // Firstly it says you need to return an array of lots, so let's make an empty array to get started, and make sure we're returning it at the end:

    $lots = array();

    // The $lots array needs to filter through each House object, so better use a foreach loop:

    foreach () { 
      // Now run a conditional to check whether the $color parameter matches the House->roof_color OR House->wall_color properties

      if ( ) {
        // If true, add the House->lot property to the $lots array
        $lots[] = 
      }
    }

    return $lots; 
  }
}


?>

Hopefully the above clears up what the question is asking and how to go about completing it.

Adam Duffield
Adam Duffield
30,494 Points

Hey Joel,

It wasn't the answer I was after or the logic I was struggling with. Hence why I gave you the best answer! I've completed the task now but I think there was two things that through me off.

  1. I was pretending that the $house array had values to loop over, I've never had to pretend fake Key's or Values exist on Treehouse to date to pass a challenge.

  2. I think a "lot" is an American term used with housing? Either that or I don't know enough about housing to understand what a lot is just yet which had me confused with the task at the mention of "lot".

Anyway thanks for the support! and hey, small world! I work in Leeds and you live in Leeds what are the odds! Maybe I'll spot you at the next Code in the Dark Competition in October?

Thanks,

Adam

Joel Bardsley
Joel Bardsley
31,246 Points

Thanks Adam, I've been wanting to give the Code in the Dark competition a try so we'll see!

<?php

// add code below this comment class Subdivision { public $houses= array(); public function filterHouseColor($color){ foreach($this->houses as $house){ if(($house->roof_color === $color) || ($house->wall_color === $color)) $houseHolds[]=$house; } return $houseHolds; } } ?>

I agree with the confusion of the wording. Since we worked with length and width previously in this course I was assuming that 'parameter' meant the actual parameter of the lot the house was built on and not a parameter (or argument) of a function. Plus 'lot' confused me with are we passing the length x width (again) or some other measurement of size such as square feet. Please reword this challenge so its clear what exactly we are supposed to be returning. Thanks.

Alex Misnovs
Alex Misnovs
12,163 Points

Hey guys, just wanted to drop my 2 cents here since I ran into same issue as Adam, however In my case I was returning inside a foreach loop rather then outside of it.

However, the problem is in the error reporting/feedback what challenge system uses. For example, in my case it was complaining about me NOT comparing both properties with the $color value when issue was with the function itself not the if statement.. What made me really confused and I ended up here. :)

P.S. Nurrokhman why would you use === instead of == in your answer If we are expecting a string not a boolean or int?

This was my answer. I think it does the same as mentioned above, but may be clearer to understand... hopefully.

<?php

class Subdivision {
  public $houses = [];

  public function filterHouseColor($color) {
    foreach ($this->houses as $house) {
      if ($house->roof_color == $color or $house->wall_color == $color) {
        $collection[] = $house;
      }
    }
    return $collection;
  }
}

?>