0

Can someone convert this into Switch Statement plz?

if(angle > 0 && angle < 90) { result = "Acute angle" } else if (angle == 90) { result = "Right angle."; } else if (angle >90 && angle < 180) { result = "Obtuse angle."; } else { result = "straight angle" }

10th May 2021, 2:33 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar
16 odpowiedzi
+ 6
Saddam Akbar Khan Did you mean to say "Now working" or "Not working"? This is actually a trickier scenario for converting if-else statements that involve a range of values. Switch takes a value and matches against possible values on a case by case basis. If your value is a number, then case values should be numbers. However, in your example: case (angle > 0 && angle < 90): This evaluates to a boolean (true | false) instead of a number. So... any angle other than 0 will match on a case that evaluates as truthy. An angle of 0 will match any case that is falsey. Therefore, the results here will always fall to the default case. To accomplish what you want, you need to match on switch(true), not switch(angle). Your example would be something like the snippet below: switch (true) { case (angle === 90): result = "Acute" break default: ... } Here's a simple test to validate the ranges using both if-else and switch: https://code.sololearn.com/cwgY9HcCuxwI/?ref=app
10th May 2021, 3:58 PM
David Carroll
David Carroll - avatar
+ 5
I'm glad I could help. 👌 You'll get there in no time. Just keep coding as much and as often as possible. It only seems impressive because you're learning it all for the first time. This is still the super basic stuff. TBH... I rarely get to explore anything remotely impressive or advanced in this community that comes close to the most things we do on actual projects. It would be too difficult to even try to explain in these forums. 😉
10th May 2021, 6:20 PM
David Carroll
David Carroll - avatar
+ 3
Saddam Akbar Khan You need to uncomment line 5. It's better if you declare the result variable with let. Also... line 20 makes no sense.
10th May 2021, 4:48 PM
David Carroll
David Carroll - avatar
+ 3
Saddam Akbar Khan The issue is with line 7. If the solution isn't obvious, take the time to review the explanation in my original answer for the clue you need to resolve this. 😉
10th May 2021, 5:00 PM
David Carroll
David Carroll - avatar
+ 3
There's nothing in my actual answer that refers to ES6. Review the specific sentence from my answer: "To accomplish what you want, you need to match on switch(true), not switch(angle)." Does that clarify what the solution is?
10th May 2021, 5:11 PM
David Carroll
David Carroll - avatar
+ 2
Just convert it step by step. it's not that much hard. https://www.sololearn.com/learn/JavaScript/1139/?ref=app
10th May 2021, 2:36 PM
Genuine Stalwart
Genuine Stalwart - avatar
+ 2
You need to change the lines below from: case (angle === 180): return "Straight Line" default: return "Out of Range" to: case (angle === 180): result = "Straight Line" break default: result = "Out of Range" break Otherwise, the function will exit before updating the page.
10th May 2021, 5:22 PM
David Carroll
David Carroll - avatar
+ 2
Impressive. Someday I'd be an expert like you. Thank you so much :cD
10th May 2021, 5:46 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar
+ 2
Absolutely. Thank you so much. I will be in touch 😀
10th May 2021, 6:31 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar
+ 1
switch (angle) { case angle > 0 && angle < 90: result = "aaaa" break; } Just like this?
10th May 2021, 2:40 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar
+ 1
Yup
10th May 2021, 2:46 PM
Genuine Stalwart
Genuine Stalwart - avatar
+ 1
Thanks I am checking this link. Thank you but please can you see my code I want it like this like a user input too https://code.sololearn.com/Wwmp8cgXt12V/?ref=app
10th May 2021, 4:09 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar
+ 1
It's not working. Switch Statement does not work for my code. It's confusing now
10th May 2021, 4:54 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar
+ 1
Can you please check if I did it right? https://code.sololearn.com/Wwmp8cgXt12V/?ref=app
10th May 2021, 5:17 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar
0
Not working
10th May 2021, 2:47 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar
0
Yes that's very advance ES6 Javascript. I just finished half course of plain JS here on sololearn.
10th May 2021, 5:03 PM
Saddam Akbar Khan
Saddam Akbar Khan - avatar