Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorter way to write Jquery If Statement with multiple options for the "IF"

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!

like image 969
WebStar101 Avatar asked Jul 09 '15 05:07

WebStar101


People also ask

How to write multiple if condition in JavaScript?

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.

What is $$ in jQuery?

1. $$ has no significance to jQuery; its use is an arbitrary decision by whomever authored whatever it is your looking at.

How do you write a if loop in jQuery?

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.

How to write to conditions in if?

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 () {} .


1 Answers

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
like image 56
Mritunjay Avatar answered Oct 19 '22 04:10

Mritunjay