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

michael edmondson
michael edmondson
4,511 Points

Ive been struggling with this for a week now and in the course a excample for this was not given.

could someone who has experience walk me through this ive been stuck for sometime i feel like im close but then i feel like im way off

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            for(double i = 0; i < frogs.length; i++)
            {
              return i.Average();  
            }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

Steven Parker
Steven Parker
231,269 Points

Perhaps these hints will help:

  • the correct spelling of the "Length" property is with a capital "L"
  • it's more common to use an int as a loop index
  • numbers (either doubles or ints) have no "Average" method
  • an unconditional "return" inside a loop will end the function on the very first iteration
  • a good use of the loop might be to accumulate a total of the lengths
  • after the loop ends, the average could be determined by dividing the total by the array length
michael edmondson
michael edmondson
4,511 Points

namespace Treehouse.CodeChallenges { class FrogStats { public static double GetAverageTongueLength(Frog[] frogs) { total += frogs[i].TongueLength; for(int i = 0; i < frogs.Length; i++) { return total/frogs.length;
} } } } am i close?

Steven Parker
Steven Parker
231,269 Points

Closer, but you need to declare the total before the loop, accumulate the total inside the loop, and return the average after the loop ends.

Also, be sure to use Markdown formatting when posting code.