## 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.

# Return the average length of the tongues of the frogs in the array. Use a foreach loop as part of your solution.

I'm ashamed to say i'm completely lost. I've looked at this for ages. re-watched the video time and time again and i still can't fathom it u.u

Would anyone be willing to explain to me how this works exactly?

Is it ok if I share a correct code? (question to Treehouse Forum admins) I am so happy to solve that I can't hold myself from sharing. Because there are cases where I couldn't find a correct answer in Treehouse forum and it makes me struggle.

```namespace Treehouse.CodeChallenges
{
class FrogStats
{
public static double GetAverageTongueLength(Frog[] frogs)
{
double total = 0;
foreach(Frog frog in frogs)
{
total += frog.TongueLength;
}
}
}
}
```

where does the singular 'frog' come from? namespace Treehouse.CodeChallenges { class FrogStats { public static double GetAverageTongueLength(Frog[] frogs) { double total = 0;

```        foreach(Frog frog in frogs)
{
total+=frog.TongueLength;
}
}
}
```

}

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

For those wondering where TongueLength came from the Frog.cs file. There is two file to this exercise, so by using .TongueLength you can using the get property of Tongue Length. For reference the Frog.cs file is

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

public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
}
}
``` ```        public static double GetAverageTongueLength(Frog[] frogs)
{

Frog frog = new Frog(7);
int sum = 0;
int count = 0;

foreach(Frog f in frogs)
{
sum += f.TongueLength;
count++;
}

int avg = sum / count;
return avg;

}
```

What's the reason for creating a new Frog at the beginning of the method? Other than that, the only change I'd suggest is getting rid of the avg variable for efficiency, since you only use it once. Instead, you could just return sum / count. You know how to do an average, right? You add up all the values and then divide by the number of things.

In this case, the "things" are the frog tongues and the values are the tongue lengths.

Your function is going to get a list of Frog objects. In this list, every Frog has a TongueLength. So if you create a loop that takes each one and adds it to a total, and then after the loop divides it by the number of Frogs, you'd have an average length.

Do you have a better idea about what to do now?

I want to say yes but no... Not a clue >.< I feel like Mondays are not good days to learn u.u Try this answer it will work

``` double total=0;
for(int i=0;i<frogs.Length;i++)
{
total+=frogs[i].TongueLength;
}
```

# Enjoy Coding

This answer would not work because it is supposed to be a `foreach` loop.

thank you Anil you made it so much easier to understand. :) I did not quite understand a few basic things... please keep in mind that I will start this whole course again as well as going through the basis again, but I get very confused when I see using variables that we did not initialize in a way that I understand: For example, I get the meaning of the foreach loop, but in anil rahman's code where did the "f" come from? I will study more but a quick answer would be much appeciated Sorry for the silly doubt

thanks for explaining in a nice way. I believe you can skip out the count variable altogether here and just use "frogs.Length" for the size of the array.

With regards to the f question, I'm still learning and know more in the VBA world but I believe it's just a placeholder name so you can reference the Frog within the loop and won't be used outside the loop. So could easily have also said,

foreach(Frog kermit in frogs)

or could have said,

foreach(Frog meh in frogs)

as long as you then said kermit.TongueLength or meh.TongueLength respectively.

My question is: where does this .TongueLength come from in the body of the loop. The function's name is GetAverageTongueLength.