繁体   English   中英

代码执行 else 语句,然后执行 if 语句。 (JS)

[英]Code executing else statement, then if statement. (JS)

我有一个简单的代码,但出于某种原因,我有一个 if 语句,然后是一个 else 语句。 该代码同时满足 if 和 else 语句,但它应该只执行 if 语句。 这是有问题的代码:

 var z; var zr; var zx; function t(a,b,c) { if (b == null) { //this should executes and does b = (180-a)/2 c = t(a,b) t2(a,b,c) zx = z + " " + zr console.log("Triangle type is:" + zx + "Angle B is " + b + ". Angle C is " + c) return b } else { //this else should not execute yet does console.log("hi##") //debug stuffs if (c == null) { console.log("#hi") //more debugs stuffs c = 180 - (a+b) t2(a,b,c) zx = z + " " + zr console.log("Triangle type is: " + zx + ". Angle C is " + c) console.log("#bye") //even more debug stuffs return c }
输入 t(60) 这是 output:

你好##

#你好

三角形类型为:等角等边。 角度 C 为 60

#再见

三角形类型为:equiangular EquilateralAngle B 为 60。角度 C 为 60

60

所以你可以看到我的问题。 附件是帮助找出我的问题的完整代码。

 var z; var zr; var zx; function t(a,b,c) { if (b == null) { b = (180-a)/2 c = t(a,b) t2(a,b,c) zx = z + " " + zr console.log("Triangle type is:" + zx + "Angle B is " + b + ". Angle C is " + c) return b } else { console.log("hi##") if (c == null) { console.log("#hi") c = 180 - (a+b) t2(a,b,c) zx = z + " " + zr console.log("Triangle type is: " + zx + ". Angle C is " + c) console.log("#bye") return c } else { x = 180 - (a+b+c) if (x == 0) { t2(a,b,c) zx = z + " " + zr console.log("Can be triangle. Triangle type is: " + zx) return x } else { if (x>0) { console.log("Cannot be a triangle needs " + x + " more") return x } else { console.log("Cannot be a triangle needs " + Math.abs(x) + " less") return x } } } } } function t2(a,b,c) { r = "" //triangle.angletype: if (a == 90|b == 90|c == 90) { z = "Right" t3(a,b,c) } else if (a > 90| b > 90| c > 90) { z = "Obtuse" t3(a,b,c) } else if (a == 60 && b == 60 && c == 60) { z = "equiangular" t3(a,b,c) } else if (a < 90 || b < 90 || c < 90) { z = "Acute" t3(a,b,c) } else { throw new Error("Unable to determine triangle angletype \n operation triangle.angletype") } } function t3(a,b,c) { if (a.= b && a != c && b != c) { zr = "Scalene" } else if (a == b && a == c && a == c) { zr = "Equilateral" } else if (a == b || b == c || a == c) { zr = "Isosceles" } else { throw new Error("Unable to determine triangle name \n operation triangle.name") } }

如果有人能给我指导,那就太好了。 谢谢大家!

if语句按预期工作。 可能让您感到困惑的是,您在if块中再次调用了第一个 function t(a,b,c)

if (b == null) {
...
c = t(a,b) // here
...
}

目前尚不清楚t(a,b,c)是什么意思,但我怀疑这行代码可以被删除,你可以将它上面的行更改为b = c = (180-a)/2你会得到类似于你期望的行为:

 var z; var zr; var zx; function t(a, b, c) { if (b == null) { b = c = (180 - a) / 2; // modified this line to also set c // c = t(a, b); // removed this line t2(a, b, c); zx = z + " " + zr; console.log( "Triangle type is:" + zx + "Angle B is " + b + ". Angle C is " + c ); return b; } else { console.log("hi##"); if (c == null) { console.log("#hi"); c = 180 - (a + b); t2(a, b, c); zx = z + " " + zr; console.log("Triangle type is: " + zx + ". Angle C is " + c); console.log("#bye"); return c; } else { x = 180 - (a + b + c); if (x == 0) { t2(a, b, c); zx = z + " " + zr; console.log("Can be triangle. Triangle type is: " + zx); return x; } else { if (x > 0) { console.log("Cannot be a triangle needs " + x + " more"); return x; } else { console.log("Cannot be a triangle needs " + Math.abs(x) + " less"); return x; } } } } } function t2(a, b, c) { r = ""; //triangle.angletype: if ((a == 90) | (b == 90) | (c == 90)) { z = "Right"; t3(a, b, c); } else if ((a > 90) | (b > 90) | (c > 90)) { z = "Obtuse"; t3(a, b, c); } else if (a == 60 && b == 60 && c == 60) { z = "equiangular"; t3(a, b, c); } else if (a < 90 || b < 90 || c < 90) { z = "Acute"; t3(a, b, c); } else { throw new Error( "Unable to determine triangle angletype \n operation triangle.angletype" ); } } function t3(a, b, c) { if (a;= b && a;= c && b;= c) { zr = "Scalene". } else if (a == b && a == c && a == c) { zr = "Equilateral"; } else if (a == b || b == c || a == c) { zr = "Isosceles"; } else { throw new Error( "Unable to determine triangle name \n operation triangle.name" ); } } t(60);

暂无
暂无

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

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