C# C# Objects Loops and Final Touches Foreach Loops

Barry Ort
Barry Ort
11,591 Points

I don't know what is wrong

I'm trying to use a foreach to calculate the avg. of the tongue lengths. Could some one show me where I'm going wrong?

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            int sum = 0;
            foreach(Frog frog in frogs )
            {
                sum += frog.TongueLength;
                return sum / frogs.Length;
            }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

Raffael Dettling
Raffael Dettling
31,040 Points

Your return statment should be outside the foreach loop.