C# C# Objects Loops and Final Touches Foreach Loops

Joseph Salinas
Joseph Salinas
9,355 Points

Foreach loop

What am I missing here?

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

            foreach(Frog frog in frogs)
            {
                return totalLength / frogs.Length;
            }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

Ariel Rzeszowski
Ariel Rzeszowski
2,970 Points

Take look for your loop. Use this loop for each item on the Aray. Intead when you use a return statement the method GetAverageTongueLength back a result. You have to sum of TongueLength every frog and then return a division of this sum by frogs.Length

Here is a correct code

        foreach(Frog frog in frogs)
        {
            totalLength += frog.TongueLength; //  You adding here a TongueLength of each Frog in frogs.
        }

            return totalLeght / frogs.Length;   // if doesn't work do this: return totalLegth/(double)frogs.Length;