简体   繁体   English

我在将IF else语句与Javascript中的函数配对时遇到麻烦

[英]I am having trouble with pairing an IF else statement with a function in Javascript

function calcPyth() {
    let v1 = document.getElementById("v1").value;
    let v2 = document.getElementById("v2").value;
    let v3 = document.getElementById("v3").value;

    if ((v1 * v1) == (v2*v2)+(v3*v3)) {
        alert("These are pythagoras triplets");
    } else if ((v1 * v1) < (v2*v2) + (v3*v3)) {
        alert("This is an obtuse triangle");
    } else {
        alert("This is an acute triangle");
    }
}

When I try to run to run the statement, it only returns the else if value.I have no idea what to do. 当我尝试运行该语句时,它仅返回else if值。我不知道该怎么做。

You need to check all possible combination for Pythagoras triplet. 您需要检查毕达哥拉斯三联体的所有可能组合。

 function calcPyth() { let v1 = parseInt(document.getElementById("v1").value); let v2 = parseInt(document.getElementById("v2").value); let v3 = parseInt(document.getElementById("v3").value); if ((v1 * v1) == (v2*v2)+(v3*v3) || (v2 * v2) == (v1*v1)+(v3*v3) || (v3 * v3) == (v1*v1)+(v2*v2)) { alert("These are pythagoras triplets"); } else if ((v1 * v1) < (v2*v2) + (v3*v3)) { alert("This is an obtuse triangle"); } else { alert("This is an acute triangle"); } } 
 <input type="text" id="v1" value="3"/><br/> <input type="text" id="v2" value="4"/><br/> <input type="text" id="v3" value="5"/><br/> <button onClick="calcPyth()">calcPyth</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM