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 trialJesse Page
Courses Plus Student 18,465 PointsI am unable to finish both the objective 'For Loops' and 'Foreach Loops' , because unable get the var tongueLength
I have tried multiple things, however I am stuck. This is my code for the objective: 'For Loops'. Thanks in advance, Jesse.
public static double GetAverageTongueLength(Frog[] frogs)
{
for (int i = 0; i > 0; i++)
{
Frog frogs = Frog[i];
return ( frog.TongueLength / frogs.Length );
}
}
2 Answers
Steven Parker
231,236 PointsThis code would not return the average of all lengths, or loop through all the frogs. Since the "return" is placed inside the loop, it would return during the very first pass.
Here's a few hints for fixing it:
- create a variable to accumulate a sum of all the lengths
- inside the loop, add the current length to the sum
- after the loop finishes, compute the average using the sum instead of just one length
- return the final average
Jesse Page
Courses Plus Student 18,465 PointsYes, I think a got a lot further. Getting a very simpel error do. Haved tried multiple things to solve it. No succes yet. This is my code.
namespace Treehouse.CodeChallenges { class FrogStats {
public double GetAverageTongueLength(Frog[] frogs)
{
double sum;
for (int i = 0; i > 0; i++)
{
sum +=frogs[i];
}
return (sum / frogs.Length);
}
}
}
The error I get: "error CS0019: Operator +=' cannot be applied to operands of type
double' and `Treehouse.CodeChallenges.Frog'"
I have tried changing the operands type, no luck...
Steven Parker
231,236 PointsYou can't add frogs, but you can add the TongueLength of each frog.