A shorter way to write a jquery if statement with multiple choices for "IF"

Is there an easier / shorter way with Jquery to write an if statement as follows:

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"; } 

A numeric variable is just an input field in which the user enters a number from 0 to 10.

Thanks!

+5
source share
13 answers

For your specific problem, you can simply write a function below:

 var getDegrees = function(number){ return -160 + ((+number)*2); } 

and name it below:

 getDegrees("1") // will return -158 getDegrees("10") // will return -140 getDegrees(10) // will also return -158 
+8
source

Um ... quick and easy with an array.

 degrees = [-160, -158, ..., -140][number]; 

Or, to avoid hard-coded values ​​in the code, try creating a function that provides a value based on a formula.

 degress = -160 + 2 * number; 
+2
source

Create an array instead of if s.

 var degrees = ["-160","-158","-156","-154","-152","-150","-148","-146","-144","-142","-140"]; 

And then you can simply derive the correct degrees with

 console.log(degrees[number]); 
+2
source

Use swich

 switch(number ) { case "0":degrees = "-160"; break; case "1" : degrees = "-158"; break; ... ... case "10" : degrees = "-140"; break; } 
+2
source

switch statement may work for you

 switch(number){ case 1: doThis(); break; case 2: doThat(); break; } 
0
source

to try

  var num=-160; if(number >= "0" and number <= "10" ){ degree = num+ (number *2); // diff is 2 } 

or you can use a simple single line

  degree = -160 + (number * 2) 
0
source

You can use the map created below and get the values ​​using the variable number

 var map = new Object(); map["0"]="-160"; map["1"]="-158"; map["2"]="-156"; map["3"]="-154"; map["4"]="-152"; map["5"]="-150"; map["6"]="-148"; map["7"]="-146"; map["8"]="-144"; map["9"]="-142"; map["10"]="-140"; 

and extract values ​​like

 degrees = map[number]; 
0
source

For me, I use like this:

 var obj = { 0 : '-160', 1 : '-158' } if ( $.inArray(number, [1,2,3,...10] ) !== -1 ) { degree = obj[number]; } 
0
source

Why aren’t you using a switch lock block?

 switch (number) { case "0": degrees = "-160"; case "1": degrees = "-158"; . . . default: } 

However, you can convert the number to int. then you just write this command only:

 degrees = "-" + 160 - number * 2; 
0
source

Use an object instead:

  var myObject = {'0':'-160', '1':'-158', '2':'-156'}; var degrees = myObject[number]; 
0
source

Try to create an object with properties 0 - 10 , set degrees as the value of the property inside the object

 var obj = { "0": "-160", "1": "-158", "2": "-156", "3": "-154", "4": "-152", "5": "-150", "6": "-148", "7": "-146", "8": "-144", "9": "-142", "10": "-140" }; // set pseudo-random number var number = 1 + Math.floor(Math.random() * 10); // set `degrees` var degrees = obj[number]; document.body.textContent = degrees; 
0
source

How about this?

 degrees = ( -160 + 2 * number ).toString(); 

 var number = "3"; var degrees = ( -160 + 2 * number ).toString(); console.log( number, degrees ); 
0
source

So, there are several ways to answer this. This is not exactly shorter, but your intentions may be more obvious and direct with switch rather than if s:

 switch (number) { case "0": degrees = "-160"; break; case "1": degrees = "-158"; break; ... } 

But for this type, the lookup table can be more understandable and concise:

 var degreesLookupTable = { "0": "-160", "1": "-158", "2": "-156", "3": "-154", "4": "-152", "5": "-150", "6": "-148", "7": "-146", "8": "-144", "9": "-142", "10": "-140" }; if (number in degreesLookupTable) degrees = degreesLookupTable[number] else throw "Number not found in degree map!" 

See the “Search Tables” section here for more information.

Thirdly, there seems to be a direct mathematical relationship between number and degrees . Maybe just write a function to calculate degrees directly based on the value of number ?

0
source

All Articles