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" }
16 ответов
+ 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
+ 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. 😉
+ 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.
+ 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. 😉
+ 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?
+ 2
Just convert it step by step. it's not that much hard.
https://www.sololearn.com/learn/JavaScript/1139/?ref=app
+ 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.
+ 2
Impressive. Someday I'd be an expert like you. Thank you so much :cD
+ 2
Absolutely. Thank you so much. I will be in touch 😀
+ 1
switch (angle) {
case angle > 0 && angle < 90:
result = "aaaa"
break;
}
Just like this?
+ 1
Yup
+ 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
+ 1
It's not working. Switch Statement does not work for my code. It's confusing now
+ 1
Can you please check if I did it right?
https://code.sololearn.com/Wwmp8cgXt12V/?ref=app
0
Not working
0
Yes that's very advance ES6 Javascript. I just finished half course of plain JS here on sololearn.