Is this a triangle? help

Hi. I’m having just a little trouble with this kata. The link to the challenge is here: https://www.codewars.com/kata/56606694ec01347ce800001b/train/javascript

The instructions are:

Implement a method that accepts 3 integer values a, b, c. The method should return true if a triangle can be built with the sides of given length and false in any other case.

(In this case, all triangles must have surface greater than 0 to be accepted).

Here is my code so far:

function isTriangle(a,b,c) {
  if ((Math.pow(a, 2) + Math.pow(b, 2) === Math.pow(c, 2)) ||
(Math.pow(b, 2) + Math.pow(c, 2) === Math.pow(a, 2)) ||
(Math.pow(c, 2) + Math.pow(a, 2) === Math.pow(b, 2))) {
   return true;
  } else {
    return false;
  } //close if/else
} //close function

//psuedocode
//accept input
//check for all iterations of a^2 + b^2 = c^2
//if matches, return true
//else, return false

The part that is messing with me is that it’s passing several of its tests. Any help helping to get it to pass the rest would be appreciated! Thanks!

It looks to me like you are actually testing isRightAngledTriangle(). There are some vids on the net showing how to determine the ‘triangularness’ of three lines. I didn’t watch, but this looks like it might help: https://www.youtube.com/watch?v=NGHZVjYqA2s.

hth

1 Like

Oh, right. I suspected the issue was with the math not with the syntax (this time). Thanks for the catch!

I think it’s just if the lengths of sides a + b are greater than c, it can’t be a triangle, which should be easy enough to implement. Thanks again!

That’s what I originally thought, but when I first searched, the results were all about finding a triangle from three points (which includes determining if one point is on a line between two other points) and I thought it might be trickier to solve.

glad i could help :slight_smile:

Ok, so after a little more tinkering, I got this:

function isTriangle (a, b, c) {
  if ((a + b < c) || (a + c < b) || (b + c < a)) {
    return false;
  } //closes if statement
  else {
    if ((a <= 0) || (b <= 0) || (c <= 0)) {
    return false;
    } //closes nested if statement
    else {
    return true;
    } //closes nested else statement  
  } //closes else statement 
}//closes function isTrangle

It’s passing 13 of the 16 tests. What cases am I forgetting?

As a follow-up, can you still console.log() in CodeWars? I’m trying to use that to figure out what values they’re passing for a, b, and c, but none of my logs are showing up.

Your issue is here:
if ((a + b < c) || (a + c < b) || (b + c < a)) {

triangle inequality theorem: which states that the sum of the side lengths of any 2 sides of a triangle must exceed the length of the third side.

example:
a = 7
b = 2
c = 2.

b + c > a
2 + 2 > 7 // false

Part 2 of your question:
The output should display in the left side where the output shows.

Hey, thanks!

This is what I ended up using for the answer:

function isTriangle (a, b, c) {
  if ((a + b <= c) || (a + c <= b) || (b + c <= a)) {
    return false;
  } //closes if statement
  else {
    if ((a <= 0) || (b <= 0) || (c <= 0)) {
    return false;
    } //closes nested if statement
    else {
    return true;
    } //closes nested else statement
  } //closes else statement
}//closes function isTrangle

I was forgetting the <= cases in the if statement.

Yeah, I’m aware that the output appears in the output window. I’m more concerned with whether Code Wars allows console.logs or if not because mine weren’t showing up.

Btw., you’ll find it much easier to type the code as the directions describe it.

But good job for figuring it out. :smiley: