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 trialRasmus Flomén
3,193 PointsHelp!! code callenge "Return the average length of the tongues of the frogs in the array. Use a for loop as part of you"
I don't relly know Whats wrong! added error masseg in the FrogStats
namespace Treehouse.CodeChallenges
{
class FrogStats
{
public static double GetAverageTongueLength(Frog[] frogs)
{
int AverageTongueLength = 0;
for(int i = 0; i < frogs.Length; i++){
Frog frog = frogs[i];
if(frog.TongueLength = null){
AverageTongueLength += 0;
}
AverageTongueLength += frog.TongueLength;
}
}
}
}
/* error masseg
FrogStats.cs(10,26): error CS0200: Property or indexer `Treehouse.CodeChallenges.Frog.TongueLength' cannot be assigned to (it is read-only)
Compilation failed: 1 error(s), 0 warnings*/
namespace Treehouse.CodeChallenges
{
public class Frog
{
public int TongueLength { get; }
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
}
}
3 Answers
Jennifer Nordell
Treehouse TeacherI posted a solution and explanation of this challenge just a day or two ago. Take a look (you might have to scroll down a bit). Hope this helps!
https://teamtreehouse.com/community/flat-out-lost
edited for additional note
The reason you're getting a compiler error is this line:
if(frog.TongueLength = null){
You need a double equals sign there. The way you have it is an assignment, not a comparison.
Yi Chiang
11,738 PointsYou can try use double instead of int. It is asking about average. it might try to add total length in array and divided it by array length.
namespace Treehouse.CodeChallenges
{
class FrogStats
{
public static double GetAverageTongueLength(Frog[] frogs)
{
double total=0;
for(int i=0;i<frogs.Length;i++)
{
total+=frogs[i].TongueLength;
}
return total/frogs.Length;
}
}
}
Brooke Neace
11,715 PointsI like this answer the best, and is close to what I came up with. The only thing I don't understand is where the .TongueLength; comes from. Can you elaborate on why that is needed? My compiler doesn't work without it and says the double cannot use the += operator but when I add .TongueLength it works, I don't fully understand why it's needed other than it works. Thanks.
Unsubscribed User
2,446 Points{ total+=frogs[i].TongueLength; } return total/frogs.Length;
Could you explain these lines of code to me? I'm having trouble comprehending the syntax.
Elfar Oliver
3,924 PointsHow do you come up with this? Do you remember every rule?
Rasmus Flomén
3,193 PointsThank you Jennifer! :)
Yes ther is some typos "==" , "AverageTongueLength" can't de a "int" and i forgot the return statment and I misse one "}" :p