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 trialSteve Agusta
6,221 PointsC# Objects For Loop Challenge
I am having trouble with this challenge, getting an error saying I cannot implicitly convert type double to int, but I have declared the variable as a double:
namespace Treehouse.CodeChallenges
{
class FrogStats
{
public static double GetAverageTongueLength(Frog[] frogs)
{
double sum = 0;
double average = 0;
for(double i = 0; i < frogs.Length; i++)
{
sum = i;
average = sum / frogs[i].TongueLength;
return average;
}
}
}
}
2 Answers
Khaled AbuAlqomboz
Courses Plus Student 1,282 Pointsin the loop, change the iterator type from double to int, then it will work,
from: for(double i = 0; i < frogs.Length; i++)
to: for(int i = 0; i < frogs.Length; i++)
because the array index is integer.
Jeremy McLain
Treehouse Guest TeacherThe code has a return inside the for loop. This will cause the loop to only run once and then immediately return from the method. Move the return statement to after the end of the for loop so that you're returning the average after the loop has completed calculating it.
Steve Agusta
6,221 PointsSteve Agusta
6,221 PointsI'm getting the following error now:
: not all code paths return a value FrogStats.cs(10,46): warning CS0162: Unreachable code detected Compilation failed: 1 error(s), 1 warnings