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

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
231,269 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
231,269 Points

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