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

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