Is there an easier / shorter way with Jquery for writing an if statement like this:
if(number === "0" ) { degrees = "-160"; }
if(number === "1" ) { degrees = "-158"; }
if(number === "2" ) { degrees = "-156"; }
if(number === "3" ) { degrees = "-154"; }
if(number === "4" ) { degrees = "-152"; }
if(number === "5" ) { degrees = "-150"; }
if(number === "6" ) { degrees = "-148"; }
if(number === "7" ) { degrees = "-146"; }
if(number === "8" ) { degrees = "-144"; }
if(number === "9" ) { degrees = "-142"; }
if(number === "10") { degrees = "-140"; }
The number variable is just an input field where the user enters a number from 0 - 10.
Thank you!
We can also write multiple conditions inside a single if statement with the help of the logical operators && and | | . The && operators will evaluate if one condition AND another is true. Both must be true before the code in the code block will execute.
1. $$ has no significance to jQuery; its use is an arbitrary decision by whomever authored whatever it is your looking at.
The syntax for if…if (condition) statement; else statement; If condition is true, the first statement is executed and If condition is false, the second statement is executed.
An if statement is written with the if keyword, followed by a condition in parentheses, with the code to be executed in between curly brackets. In short, it can be written as if () {} .
For your specific problem you can just write a function as bellow
var getDegrees = function(number){
return -160 + ((+number)*2);
}
and call it like bellow
getDegrees("1") // will return -158
getDegrees("10") // will return -140
getDegrees(10) // will also return -158
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With