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

Dmitri Mikhalev
Dmitri Mikhalev
2,695 Points

Avarage toungue length

I am lost... Obviously I am not getting how to get the toungue lengths from Frogs.cs?

can anybody help me please?

FrogStats.cs
namespace Treehouse.CodeChallenges
{
//Return the average length of the tongues of the frogs in the array. Use a for loop as part of your solution.
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {

            int totalTongueLength = 0;
            for(int i = 0; i < frogs.Length; i++)
            {
               totalTongueLength += frogs[i];

            }
           return totalTongueLength / frogs.Length;
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

Caleb Kemp
Caleb Kemp
12,649 Points

Right now with your code totalTongueLength += frogs[i]; your code is trying to add up the number of frogs to your totalTongueLenth variable which is not what you want. To access the property of a object, we use the dot notation.

For example, if your frogs had a color property, you could get that value by writing frogs[i].color.

Your almost there, He covered how to do this in this previous lesson at around minute 2:50. Hope that helps!

Dmitri Mikhalev
Dmitri Mikhalev
2,695 Points

Oh, it was just that... Thanks alot! :)