Home > Back-end >  if statement ignoring inequality
if statement ignoring inequality

Time:04-17

first time posting here so sorry if this isn't as clear or efficient as can be, but I'm trying to write a program to take user inputs for 3 sides of a triangle and see if they are a valid triangle based on these rules:

a b > c

a c > b

b c > a

So far, I'm just trying to prove a b > c (which I called condition1). But in trying to solve this, it seems that the if statement I have is ignoring the a b part and just checking a > c. Can anyone tell me what I'm doing wrong?

function isTriangle() {
  var input1 = document.getElementById("a");   // fetch the 3 numbers
  var input2 = document.getElementById("b");
  var input3 = document.getElementById("c");
  var displayAnswer = document.getElementById("display");

  function condition1(){
    if ((input1.value   input2.value) > input3.value)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  //our goal here is to use triangle inequality theorem
if (condition1(true)) {
  displayAnswer.innerHTML = "These three sides can form a   right triangle!";
}
else {
  displayAnswer.innerHTML = "These three sides cannot form a   right triangle!";
}


}
<!DOCTYPE html>
<html>
<head>
    <title>Project 3</title>
    <script src="proj3.js" type="text/javascript"></script>
</head>

<body>
<h1>Triangle Tester</h1>

  <p> Please input the length (integer) of the first side of the triangle: </p>
  <input id="a" type="text" size="3" /> 

  <p> Please input the length (integer) of the second side of the triangle: </p>
  <input id="b" type="text" size="3" /> 

  <p> Please input the length (integer) of the third side of the triangle: </p>
  <input id="c" type="text" size="3" /> 

  <br />
  <br />
  <span id="display"></span>

  <br />

  <button onclick="isTriangle();">(2)</button>

</body>
</html>

CodePudding user response:

Probably that's because you didn't parse the input value to a number.

When you do input1 input2 it hust concatenate the strings

This works as expected

function isTriangle() {
  var input1 = document.getElementById("a");   // fetch the 3 numbers
  var input2 = document.getElementById("b");
  var input3 = document.getElementById("c");
  var displayAnswer = document.getElementById("display");

  const condition = parseInt(input1.value)   parseInt(input2.value) > parseInt(input3.value)

  //our goal here is to use triangle inequality theorem
if (condition) {
  displayAnswer.innerHTML = "These three sides can form a   right triangle!";
}
else {
  displayAnswer.innerHTML = "These three sides cannot form a   right triangle!";
}

  
}
<!DOCTYPE html>
<html>
<head>
    <title>Project 3</title>
    <script src="proj3.js" type="text/javascript"></script>
</head>
  
<body>
<h1>Triangle Tester</h1>

  <p> Please input the length (integer) of the first side of the triangle: </p>
  <input id="a" type="text" size="3" /> 

  <p> Please input the length (integer) of the second side of the triangle: </p>
  <input id="b" type="text" size="3" /> 

  <p> Please input the length (integer) of the third side of the triangle: </p>
  <input id="c" type="text" size="3" /> 
  
  <br />
  <br />
  <span id="display"></span>

  <br />

  <button onclick="isTriangle();">(2)</button>
 
</body>
</html>

CodePudding user response:

As others have already suggested, I request you to add more details. As R4ncid has already pointed out, you need to convert the value to integer. Based on my analysis of your code logic, I found an issue. For three given sides to form a triangle, all three inequality conditions should be satisfied at the same time. Hence, here's the rectified version of your code:

function isTriangle() {
  var input1 = document.getElementById("a");   // fetch the 3 numbers
  var input2 = document.getElementById("b");
  var input3 = document.getElementById("c");
  var displayAnswer = document.getElementById("display");
  var a = parseInt(input1.value);
  var b = parseInt(input2.value);
  var c = parseInt(input3.value);
  // Covering all possible conditions
  var inEqualityCond = ((a   b) > c && (b   c) > a && (a   c) > b) ? true : false;
  function condition1(){
    if (inEqualityCond)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  //our goal here is to use triangle inequality theorem
if (condition1(true)) {
  displayAnswer.innerHTML = "These three sides can form a   right triangle!";
}
else {
  displayAnswer.innerHTML = "These three sides cannot form a   right triangle!";
}


}
<head>
    <title>Project 3</title>
    <script src="proj3.js" type="text/javascript"></script>
</head>

<body>
<h1>Triangle Tester</h1>

  <p> Please input the length (integer) of the first side of the triangle: </p>
  <input id="a" type="text" size="3" /> 

  <p> Please input the length (integer) of the second side of the triangle: </p>
  <input id="b" type="text" size="3" /> 

  <p> Please input the length (integer) of the third side of the triangle: </p>
  <input id="c" type="text" size="3" /> 

  <br />
  <br />
  <span id="display"></span>

  <br />

  <button onclick="isTriangle();">(2)</button>

</body>
</html>

  • Related