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# C# Objects Loops and Final Touches For Loops

Syed Abbas
Syed Abbas
4,762 Points

Hi I am not able to solve the challenge task 1 for for loops. Can someone help please?

I have tried to many ways to return the average length but nothing is working.

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {   

            for(int i=0;i<frogs.Length;i++)
            {
            Frog f= new Frog(frogs[i]);
             return f;
            }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,198 Points

Maybe these hints will help:

  • the Frog constructor takes a length, not another frog
  • you wont need to make new frogs to calculate the average length
  • you might want to establish a variable to hold a total
  • inside the loop, you might add the current frog's tongue length (not the frog itself) to the total
  • after the loop finishes, you could divide the total by the number of frogs to get the average
  • return the result of the final calculation
Syed Abbas
Syed Abbas
4,762 Points

Thank you very much for your help.

Syed Abbas
Syed Abbas
4,762 Points

Thank you very much for your help.