Reputation: 187
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!
Upvotes: 5
Views: 185
Reputation: 5941
Use an object instead, like this:
var myObject = {'0':'-160', '1':'-158', '2':'-156'};
var degrees = myObject[number];
Upvotes: 0
Reputation: 9637
try
var num=-160;
if(number >= "0" and number <= "10" ){
degree = num+ (number *2); // diff is 2
}
or you can use simple one line
degree = -160 + (number * 2)
Upvotes: 0
Reputation: 24638
How about this?
degrees = ( -160 + 2 * number ).toString();
var number = "3";
var degrees = ( -160 + 2 * number ).toString();
console.log( number, degrees );
Upvotes: 0
Reputation: 24699
So, there are a few ways I could answer this. It's not exactly shorter, but your intentions may be more apparent and direct with a switch
rather than if
s:
switch (number)
{
case "0":
degrees = "-160";
break;
case "1":
degrees = "-158";
break;
...
}
But, for this type of thing, a lookup table may be more clear 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 Lookup 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
?
Upvotes: 0
Reputation: 94319
Um... Quick and easy with an array.
degrees = [-160, -158, ..., -140][number];
Or, to avoid hard-coding values into the code, try to create a function which yields the value based on a formula.
degress = -160 + 2 * number;
Upvotes: 2
Reputation: 1
Try creating an object having properties 0
- 10
, set degrees
as value of property within 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;
Upvotes: 0
Reputation: 3299
Why you dont use switch-case block?
switch (number) {
case "0":
degrees = "-160";
case "1":
degrees = "-158";
.
.
.
default:
}
However you can convert number to int. then you simply write only this command:
degrees = "-" + 160 - number * 2;
Upvotes: 0
Reputation: 115222
Use swich
switch(number ) {
case "0":degrees = "-160";
break;
case "1" : degrees = "-158";
break;
...
...
case "10" : degrees = "-140";
break;
}
Upvotes: 2
Reputation: 25882
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
Upvotes: 8
Reputation: 9060
For me, i'm like use like this :
var obj = {
0 : '-160',
1 : '-158'
}
if ( $.inArray(number, [1,2,3,...10] ) !== -1 ) {
degree = obj[number];
}
Upvotes: 0
Reputation: 28513
You can use map as created below and retrive values using number
variable
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 retrive values like
degrees = map[number];
Upvotes: 0
Reputation: 76
a switch statement might work for you
switch(number){
case 1:
doThis();
break;
case 2:
doThat();
break;
}
Upvotes: 0
Reputation: 73241
Create an array instead of the if
s.
var degrees = ["-160","-158","-156","-154","-152","-150","-148","-146","-144","-142","-140"];
And then you can simply output the correct degrees with
console.log(degrees[number]);
Upvotes: 2