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

I cant solve this Task. Can anyone give me a Hint? i dont want the result.

Pls give me only a hint. i should return an avarage of the Toungelength. But i dont know how i can get the right way to do it. I know the Things behind.

I have to Plus all the Length and then i have to divide the Result of all length with my index variable.

Only a hint pls.

Thanks 4 your Time

Jan

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            for ( int i ; i >= frogs.Length; i++)
            {
                Frog frogsss = frogs[i];
                int frogAvg += frogsss;
                return (double)frogAvg/i;
            }

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

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

2 Answers

Steven Parker
Steven Parker
231,269 Points

Good attitude! :+1: I only give hints anyway.

  • you might want to declare and initialize frogAvg before the loop
  • remember to initialize your loop variable with a value (typicallly 0)
  • the loop condition must be true for the loop to continue
  • you can't add frogs directly, but you can add their TongueLength values
  • don't return until after the loop completes
  • the loop variable "i" won't be available in the outer context (but frogs.Length will)
Jan-Morten Reiners
Jan-Morten Reiners
1,236 Points

Nice Hints,

I was be able to find the right way, I think, But now the Compiler give me a msg, with the question:

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

But i returend the result of the avarage.

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;
    }
}

}

Steven Parker
Steven Parker
231,269 Points

Perhaps one of my hints was not clear. When I said "the loop condition must be true for the loop to continue", I was talking about this line:

            for ( int i ; i >= frogs.Length; i++)

I noticed you were testing if "i" was equal or greater than the length of the frogs array. This would be be true for when you want the loop to stop. But the conditional clause of a for loop needs to be true for when you want the loop to continue, and false to make it stop. So you need to reverse the test to make it check if the indeix is less than the length of the array.

Otherwise, I think you have it. After this last change you'll pass the challenge. Good job! :+1: