A code to test whether 3 dimensions can form a triangle, or a right angled triangle

<!DOCTYPE html>
<html> 
<head>
<meta charset=utf-8 />
<title>test if three integers give a triangle or right angled triandle</title>
<style type="text/css">
body {margin: 30px;}
</style> 
</head>
<body>
  <h1> Input three integers to test if they form a triangle or even a right angled triangle</h1>
<form>
Side a: <input type="text" id="sideA" /><br>
Side b: <input type="text" id="sideB" /><br>
Side c: <input type="text" id="sideC" /><br>
<input type="button" onClick="testTriangle()" Value="test" />

</form>

</body>
  <script> 
    //takes the inbut of three integers
    function testTriangle(){
a = parseInt (document.getElementById("sideA").value);
b = parseInt (document.getElementById("sideB").value);
c= parseInt (document.getElementById("sideC").value);

if ((a + b <= c) || (a + c <= b) || (b + c <= a)) {
alert ("not a triangle");//test if the sides make a triangle
}
else {
if ((a <= 0) || (b <= 0) || (c <= 0)) {
alert ("not a triagnle");//a side length cannot be equal to zero
} 
else  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))) {
  alert ("right angled triangle")//tests if a triangle is right angled
}
else {
alert (" a triangle");//then the condition satisfies the requirements of a triangle
}
}
}
  </script>
</html>

Hello!

How much have you done? Are there any specific problems you’re having?

We can help you by teaching what’s needed (or suggest what to read), but we will not solve the problem for you :sweat_smile: (we wouldn’t be helping you in that case).

function testTriangle(){
   a = parseInt (document.getElementById("sideA").value);
   b = parseInt (document.getElementById("sideB").value);
   c= parseInt (document.getElementById("sideC").value);

  if ((a + b <= c) || (a + c <= b) || (b + c <= a)) {
  alert ("not a triangle");
  } 
  else {
    if ((a <= 0) || (b <= 0) || (c <= 0)) {
    alert ("not a triagnle")
    } //closes nested if statement
    else {
    alert ("  a triangle");
    } 
  }
}

it is working! i solved it but i now need to test if it is a right angled triangle

Nice :slight_smile:!

In that case you just need to find the longest of the three sides and apply the formula: a^2 + b^2 = c^2, where c is longest one (the hypotenuse). Note that ^ means to the power of, so a^2 means a to the power of 2.

thanks. how do I find the longest side, please?

There are different ways of doing it, the simplest one being using if...else:

if (a > b && a > c) {
  // Side a is the hypotenuse
} else if(...) { // Add the check for side b
} else { // Here would be the side c
}

I’m not sure it’s the simplest one :thinking:
How about:

// sort numbers ascending and assign to respective variables
const [c1, c2, h] = [a, b, c].sort();
// check for right angle
const isRightAngle = h * h === c1 * c1 + c2 * c2;
1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Yes, that’s a better solution :slight_smile:!