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,647 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.