Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

I 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
Steven Parker
221,323 Points

This 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

Yes, 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 typedouble' and `Treehouse.CodeChallenges.Frog'"

I have tried changing the operands type, no luck...

Steven Parker
Steven Parker
221,323 Points

You can't add frogs, but you can add the TongueLength of each frog. :wink: