C# C# Objects Loops and Final Touches Foreach Loops

LESTER POLICARPIO
LESTER POLICARPIO
2,139 Points

Return the average length of the tongues of the frogs in the array. Use a foreach loop as part of your solution.

Hi I am having this error on my code

FrogStats.cs(5,30): error CS0161: `Treehouse.CodeChallenges.FrogStats.GetAverageTongueLength(Treehouse.CodeChallenges.Frog[])': not all code paths return a value Compilation failed: 1 error(s), 0 warnings

is it because the frogs.Length value is always greater than 1 on the total frogs in the array?

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double total = 0;

            foreach (Frog frog in frogs)
            {
                total += frog.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

LESTER POLICARPIO
LESTER POLICARPIO
2,139 Points

Ohh Man already figure it out =( ..... the return should be outside the loop

Thomas Beaudry
Thomas Beaudry
29,084 Points

Wow, I had the exact same problem I had it inside the loop...