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

Jan-Morten Reiners
Jan-Morten Reiners
1,236 Points

Can anyone give me a Hint? I think iam returning the right avarage of the tounges.

Bummer! Are you returning the average length of the tongues of the frogs in the array?

These is the msg that the compiler gives to me. I think i have a small mistake in the code.

Thanks for your help

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

            for ( int i=0 ; (i >= frogs.Length); i++)
            {
                frogsToungeLength = frogs[i].TongueLength ;
                frogAvg += frogsToungeLength;


            }
            return (double)frogAvg/frogs.Length;  
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing great, but there's a problem in your for loop. You start the variable i at 0 and then say while it is greater than or equal to the length of the frogs array. But 0 is never going to be greater than the length of the frogs array, so the loop actually never runs. What you end up with is an attempt to return zero divided by zero. And of course, you can't divide by zero.

So, if I alter your for loop just sightly, your code passes. Take a look:

  for ( int i=0 ; (i < frogs.Length); i++)

It's also important to note that this must be less than and not less than or equals to. A less than or equals to will result in an out of bounds error as it will try to increment then index past the size of the array. If the frogs array holds six frogs, then the last frog will have an index of five.

Hope this helps! :sparkles: