Good day. I only understand with JavaScript , I try to write a simple squeak with conditions. But in fact, the conditions are read incorrectly - with any input data, the script gives the result of the last condition (('1 (Scalene)') . Help me figure out where I have an error! Thank you!

 function displayResult() { var a = document.getElementById('a').value; var b = document.getElementById('b').value; var c = document.getElementById('c').value; document.getElementById('result').innerHTML = calculateResult(a, b, c); } function calculateResult() { if (a == b && b == c) { return ('3 (Equilateral)') } else if (a >= (b + c) || c >= (b + a) || b >= (a + c)) { return ('4 (Error. Not a triangle)') } else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) { return ('2 (Isosceles)') } else if (a != b && b != c && c != a) { return ('1 (Scalene)') } } 
 <input type="text" name="a" id="a" placeholder="Side a"> <input type="text" name="b" id="b" placeholder="Side b"> <input type="text" name="c" id="c" placeholder="Side c"> <input type="button" id="calculate" onclick="displayResult()" value="Triangle type"> <br> <p id="result"></p> 

  • one
    check what the variables a, b, c are inside actually calculateResult - Grundy

1 answer 1

It does not work, because calculateResult() defined without the use of variables and given a, b, c are not used in it, but some other variables with unknown values ​​are used.

Correct the function calculateResult() to function calculateResult(a, b, c) :

 function displayResult() { var a = document.getElementById('a').value; var b = document.getElementById('b').value; var c = document.getElementById('c').value; document.getElementById('result').innerHTML = calculateResult(Number(a), Number(b), Number(c)); } function calculateResult(a, b, c) { if (a == b && b == c) { return ('3 (Equilateral)') } else if (a >= (b + c) || c >= (b + a) || b >= (a + c)) { return ('4 (Error. Not a triangle)') } else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) { return ('2 (Isosceles)') } else if (a != b && b != c && c != a) { return ('1 (Scalene)') } } 
 <input type="text" name="a" id="a" placeholder="Side a"> <input type="text" name="b" id="b" placeholder="Side b"> <input type="text" name="c" id="c" placeholder="Side c"> <input type="button" id="calculate" onclick="displayResult()" value="Triangle type"> <br> <p id="result"></p> 


As rightly noted in the comments, in order for the comparison to work correctly, the variables need to be translated into numbers. For example, when calling a function:

 document.getElementById('result').innerHTML = calculateResult(Number(a), Number(b), Number(c)); 
  • one
    values ​​are known: these are elements with such id - Grundy
  • Thank you, it worked))) But now, apart from the condition "3 (Equilateral)" for all other data combinations, it produces "4 (Error. Not a triangle)" - TattyQ
  • 2
    @TattyQ, because you need to transfer to numbers before the transfer to the function, otherwise the result of the addition may be surprising - Grundy
  • Everything works now. Thank you very much!!! - TattyQ