Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Loops and Final Touches For Loops

michael edmondson
michael edmondson
4,500 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
221,451 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,500 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
221,451 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.