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

olu adesina
olu adesina
23,007 Points

i think im going in the right direction but somethings wrong

i think im going in the right direction but i need some help

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            for(i=0;i<frogs.Length;i++)
            {
                 frogs.TongueLength += frogs.TongueLength  ;
                // adding up each item from the frog array 
            }
             return  frogs.TongueLength/frogs.Length;
            // divding the total by the  length to get average lenght of tongues 
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

K Cleveland
K Cleveland
21,839 Points
for(i=0;i<frogs.Length;i++) //you've got a small syntax error here -- check out that first expression
{
    //here, you'll actually need to add together the tongue length of EACH frog in the array
   //you aren't actually accessing anything in the array
   //there's a special syntax for this that you can use. 
     frogs.TongueLength += frogs.TongueLength; 
  }    

  //return the totalTongueLength/number of frogs in array(frogs.Length)
   return  frogs.TongueLength/frogs.Length;

So think about it like this:

//define a variable to store the length of each frog's tongue 
 int totalTongueLength = 0;

//make your for loop (don't forget to fix the syntax error!)
for(i = 0; i < frogs.Length; i++)
{
   //use the variable you just made +=  each frog's tongueLength
   totalTongueLength += each frog's tongueLength
  //how would you get each frog's tongueLength? 
  //you need to get to each element in the frogs array!               
}

return  totalTongueLength/frogs.Length;