简体   繁体   English

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

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

I have a simple code but for some reason, I have an if statement, then an else statement.我有一个简单的代码,但出于某种原因,我有一个 if 语句,然后是一个 else 语句。 The code satisfies both the if and the else statment but it should only execute the if statment.该代码同时满足 if 和 else 语句,但它应该只执行 if 语句。 Heres the problematic code:这是有问题的代码:

 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 }
Input t(60) Heres the output: 输入 t(60) 这是 output:

hi##你好##

#hi #你好

Triangle type is: equiangular Equilateral.三角形类型为:等角等边。 Angle C is 60角度 C 为 60

#bye #再见

Triangle type is:equiangular EquilateralAngle B is 60. Angle C is 60三角形类型为:equiangular EquilateralAngle B 为 60。角度 C 为 60

60 60

So you can see my problem.所以你可以看到我的问题。 Attched is the full code to help figure out my problem.附件是帮助找出我的问题的完整代码。

 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 anybody can give me guidence that would be wonderful.如果有人能给我指导,那就太好了。 Thanks to all!谢谢大家!

The if statements are working as you expect. if语句按预期工作。 What might be confusing you is that you're calling the first function t(a,b,c) again inside the if block:可能让您感到困惑的是,您在if块中再次调用了第一个 function t(a,b,c)

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

it's not clear what that t(a,b,c) is meant to return, but I suspect this line of code could be removed, and you could change the line above it to be b = c = (180-a)/2 and you would get behaviour similar to what you expect:目前尚不清楚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