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

Dmitri Mikhalev
Dmitri Mikhalev
4,462 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,754 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
4,462 Points

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