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 trialBabatunde Obidare
Full Stack JavaScript Techdegree Student 4,925 PointsWhy is it saying "Is your math correct?"
Add a static method named CalculateHypotenuse to the RightTriangle class that returns the length of long side of a right triangle (the hypotenuse), given the length of each of the the two shorter sides (the legs). The Pythagorean theorem will come in handy. The method should take two parameters of type double and return a double.
Bummer! Is your math correct?
namespace Treehouse.CodeChallenges { class RightTriangle { private static double a; private static double b; private static double c;
public static double CalculateHypotenuse(double sideA, double sideB)
{
sideA = a;
sideB = b;
return c = System.Math.Sqrt(sideA * sideA) + System.Math.Sqrt(sideB * sideB);
}
}
}
namespace Treehouse.CodeChallenges
{
class RightTriangle
{
private static double a;
private static double b;
private static double c;
public static double CalculateHypotenuse(double sideA, double sideB)
{
sideA = a;
sideB = b;
return c = System.Math.Sqrt(sideA * sideA) + System.Math.Sqrt(sideB * sideB);
}
}
}
4 Answers
Seth Kroger
56,414 PointsThe correct formula should be "the square root of the sum..." instead of "sum of the square roots..." You are also reassigning sideA and sideB to different values (most likely zero if there is no other way to initialize/set them).
public static double CalculateHypotenuse(double sideA, double sideB)
{
return System.Math.Sqrt(sideA * sideA + sideB * sideB);
}
Dale Bailey
20,269 PointsAnother alternative is to use Math.Pow like this
public static double CalculateHypotenuse(double sideA, double sideB) {
return System.Math.Sqrt(System.Math.Pow(sideA, 2) + System.Math.Pow(sideB, 2));
}
although you may want to add
using System;
to make it look cleaner.
Gavin Schilling
37,904 Pointsnamespace Treehouse.CodeChallenges
{
class RightTriangle
{
public static double CalculateHypotenuse (double _legOfA, double _legOfB) => System.Math.Sqrt (System.Math.Pow (_legOfA, 2) + System.Math.Pow (_legOfB, 2) );
}
}
Andrei Li
34,486 PointsYet another solution.
using System;
namespace Treehouse.CodeChallenges
{
class RightTriangle
{
public static double CalculateHypotenuse(double first, double second)
{
return Math.Sqrt(Math.Pow(first,2)+Math.Pow(second, 2));
}
}
}
Babatunde Obidare
Full Stack JavaScript Techdegree Student 4,925 PointsBabatunde Obidare
Full Stack JavaScript Techdegree Student 4,925 PointsThanks!