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

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

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

I'm stuck in deep. I've tried figuring out what's wrong but nothing comes up my mind.

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
           int frogToungeLength = 0;
           int frogTotal = 0;
            for(int i=0; i < frogs.Length; i++)
            {
               frogTongueLength = frogs[i].TongueLength;
               frogTotal++;
            }
            return frogToungeLength/frogTotal;

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

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

2 Answers

Steven Parker
Steven Parker
231,268 Points

Did you try the "preview" button? When you do, the compiler message shown is:

FrogStats.cs(11,16): error CS0103: The name `frogTongueLength' does not exist in the current context

And that's because on line 7 where it is declared, it is spelled differently ("frogToungeLength").

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

I've just fixed it, however I still receive an error: "Are you returning the average length of the tongues of the frogs in the array?"

Steven Parker
Steven Parker
231,268 Points

Did you make sure the spelling is the same in all the references? (if that's not it, show the current code)

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

Steven Parker , yes. But it is still giving me the same error. Nothing is shown under "Editor".

namespace Treehouse.CodeChallenges { class FrogStats { public static double GetAverageTongueLength(Frog[] frogs) {

        int frogTongueLength = 0;
       int frogTotal = 0;
        for(int i=0; i < frogs.Length; i++)
        {
           frogTongueLength = frogs[i].TongueLength;
           frogTotal++;
        }
        return frogTongueLength/frogTotal;

    }
}

}

Steven Parker
Steven Parker
231,268 Points

This code is just storing the last last tongue length. Change the "=" to "+=" to make it sum them instead.