简体   繁体   中英

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. The code satisfies both the if and the else statment but it should only execute the if statment. 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:

hi##

#hi

Triangle type is: equiangular Equilateral. Angle C is 60

#bye

Triangle type is:equiangular EquilateralAngle B is 60. Angle C is 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. What might be confusing you is that you're calling the first function t(a,b,c) again inside the if block:

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:

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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