我正在尝试编写代码以识别不同类型的三角形。但是激活该功能后,不会发出警报。这是我的代码:
function alertf(){
var a=document.getElementById("a").value;
var b=document.getElementById("b").value;
var c=document.getElementById("c").value;
if((sqr(a)+sqr(b)==sqr(c))||(sqr(a)+sqr(c)==sqr(b))||(sqr(b)+sqr(c)==sqr(a)))
alert("Right triangle")
else if((a==b)||(b==c)||(a==c)) alert("Isosceles triangle")
else if((a==b)&&(a==c)) alert("Equilateral triangle")
else alert("Ordinary triangle");
答案 0 :(得分:0)
我会尝试以下方法:
function sqr(x) {
return Math.pow(x, 2);
}
function showOutput(output) {
document.getElementById("output").innerHTML = output;
}
function alertf(){
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var c = document.getElementById("c").value;
if((sqr(a)+sqr(b)==sqr(c))||(sqr(a)+sqr(c)==sqr(b))||(sqr(b)+sqr(c)==sqr(a))) {
showOutput("Right triangle");
} else if((a==b)&&(a==c)) {
showOutput("Equilateral triangle");
} else if((a==b)||(b==c)||(a==c)) {
showOutput("Isosceles triangle");
} else {
showOutput("Ordinary triangle");
}
}
alertf();
Side A length: <input id="a" value="3" onchange="alertf()"></input><br>
Side B length: <input id="b" value="4" onchange="alertf()"></input><br>
Side C length: <input id="c" value="5" onchange="alertf()"></input><br><br>
Output:<div id="output"><div>