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 trialMai Lon Ross
5,891 PointsFewer LInes
Why can't I just have one line in the function, rather than having to create a new array, populate it, and then count the elements? Such as: return count(get_products_all());
3 Answers
thomascawthorn
22,986 PointsAh, code challenges are very specific in what they're looking for. I prefer your answer!
Hampton Paulk might be able to update this answer, or maybe explain why it's a little long winded.
Mai Lon Ross
5,891 PointsThis was on a code challenge. The single line was not accepted, but the three lines were. It is a function to get the count of the number of elements in an array. This is my code from Mike's store, I think the code challenge used $flavors but it is the same thing.
function get_products_count() {
$products = array();
$products = get_products_all();
return count($products);
}
versus
function get_products_count() {
return count(get_products_all());
}
thomascawthorn
22,986 PointsIf you can replace the code with one simpler, more readable line, then make it happen!
Unless there's a good reason that these variables have to be defined regardless. What's the original function?
Hampton Paulk
5,093 PointsHampton Paulk
5,093 PointsTom is right, your code is smaller for sure. The code challenges are very picky, testing you in the way it was taught. I understand the frustration for sure.