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 For Loops

Compiler error in for loop code challenge.

I keep running into a compiler error where GetAverageTongueLength has code paths that don't return values. Here's my code.

namespace Treehouse.CodeChallenges { class FrogStats { public static double GetAverageTongueLength(Frog[] frogs) { for(int i = 0; i < frogs.Length; i++) { Frog toad = frogs[i]; } } } }

namespace Treehouse.CodeChallenges { public class Frog { public int TongueLength { get; }

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

}

Again, no idea why this doesn't work. Help?

2 Answers

Steven Parker
Steven Parker
231,198 Points

:point_right: Your GetAverageTongueLength function has no return statement in it.

So there are actually no code paths that return a value, but it is incomplete in other ways also. Since the purpose is to get an average value of tongue lengths, you probably want to accumulate a total inside the loop. Then, after the loop, you could divide that total by the number of frogs to get the average. This average would be the value to return.

David Pittman
PLUS
David Pittman
Courses Plus Student 2,586 Points

I'm really surprised the foreach wasn't covered.. foreach(Invader invader in invaders)