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

Jose Perez
Jose Perez
1,338 Points

I need an explanation for this please!

Why do we need the .TongueLength after the index? Where do we get that from?

Also why does i<=frogs.Length work only i<frogs.Length?

Can someone please help me explaining?

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double total=0;
            for(int i=0;i<frogs.Length;i++)
            {
                total+=frogs[i].TongueLength;
            }
            return total/frogs.Length;
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

2 Answers

Hello!

The method GetAverageTongueLength sums the length of all frogs' tongues then divides the sum to the number of frogs in the array. Therefore, it needs to get the TongueLength of each Frog object.

Regarding to the for loop, it goes from 0 to frogs.Length because this is the proper way to iterate through indexes.

Let's say frogs.Length would be 5. The first frog is on the 0 index - frogs[0]. The last from is on the 4 index - frogs[4].

Hope it helped!

Jose Perez
Jose Perez
1,338 Points

Thank you very much. I thought since we are already passing the array in the method shouldn’t that array contain that information about the length of the tongue? If not what infomation does the array been past have then?