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 Foreach Loops

Hi, cant find average? Thank you.

namespace Treehouse.CodeChallenges { class FrogStats { public static double GetAverageTongueLength(Frog[] frogs) { foreach (Frog tongue in frogs) { return tongue.Average(); } } } }

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
             foreach (Frog tongue in frogs)
             {
            return tongue.Average();
             }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

Stephan Olsen
Stephan Olsen
6,650 Points

You need to create a variable outside of the foreach loop and instantiate it to 0. Inside the loop you need to set this variable equal to itself + the TongueLength of the frog --> frog.TongueLength Outside of the loop you need to divide the tongue length you got from the variable with the amounts of frogs in the array, you can get the amount of frogs in the array by using the Length property --> frogs.Length And then in the end you need to return it.