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 trialMatthew Egan
1,437 PointsI cann't use the "+" symbol in my code.
I am attempting to sum up the elements in an array that was not provided, and so I assumed that it was there. I tried creating the array, but it broke. I think that this is the correct way to add up an array, and then divide by the number of elements. Where am I going wrong here?
using System;
namespace Treehouse.CodeChallenges
{
class FrogStats
{
public static double GetAverageTongueLength(Frog[] frogs)
{
double i = 0;
double total = 0;
for(; (int)i < frogs.Length; (int)i++) {
total += frogs[i];
}
double average = total / i;
return average;
}
}
}
namespace Treehouse.CodeChallenges
{
public class Frog
{
public int TongueLength { get; }
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
}
}
1 Answer
Steven Parker
231,268 PointsYou're close, but have a few issues:
- don't declare your loop index as a double and recast it, just declare it as int to begin with
- declaring the index before the loop is OK, but it's typically done in the first clause of the for
- you can't add frogs directly, but you could add their TongueLength values