Jquery是否有更简单/更短的方式来编写这样的if语句:
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"; }
数字变量只是一个输入字段,用户输入0到10之间的数字。
谢谢!
答案 0 :(得分:8)
对于您的具体问题,您只需编写一个函数,如下所示
var getDegrees = function(number){
return -160 + ((+number)*2);
}
并将其称为贝娄
getDegrees("1") // will return -158
getDegrees("10") // will return -140
getDegrees(10) // will also return -158
答案 1 :(得分:2)
嗯......使用数组快速简便。
degress = -160 + 2 * number;
或者,为了避免将值硬编码到代码中,请尝试创建一个根据公式生成值的函数。
robot pick rubbish
答案 2 :(得分:2)
创建一个数组而不是if
。
var degrees = ["-160","-158","-156","-154","-152","-150","-148","-146","-144","-142","-140"];
然后你可以用
输出正确的度数console.log(degrees[number]);
答案 3 :(得分:2)
使用swich
switch(number ) {
case "0":degrees = "-160";
break;
case "1" : degrees = "-158";
break;
...
...
case "10" : degrees = "-140";
break;
}
答案 4 :(得分:0)
switch语句可能适合你
switch(number){
case 1:
doThis();
break;
case 2:
doThat();
break;
}
答案 5 :(得分:0)
试
var num=-160;
if(number >= "0" and number <= "10" ){
degree = num+ (number *2); // diff is 2
}
或者您可以使用简单的一行
degree = -160 + (number * 2)
答案 6 :(得分:0)
您可以使用下面创建的地图,并使用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";
和反复值,如
degrees = map[number];
答案 7 :(得分:0)
对我来说,我喜欢这样使用:
var obj = {
0 : '-160',
1 : '-158'
}
if ( $.inArray(number, [1,2,3,...10] ) !== -1 ) {
degree = obj[number];
}
答案 8 :(得分:0)
为什么你不使用switch-case块?
switch (number) {
case "0":
degrees = "-160";
case "1":
degrees = "-158";
.
.
.
default:
}
但是您可以将数字转换为int。那么你只需要编写这个命令:
degrees = "-" + 160 - number * 2;
答案 9 :(得分:0)
使用对象,如下所示:
var myObject = {'0':'-160', '1':'-158', '2':'-156'};
var degrees = myObject[number];
答案 10 :(得分:0)
尝试创建具有属性0
- 10
的对象,将degrees
设置为对象中属性的值
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;
答案 11 :(得分:0)
这个怎么样?
degrees = ( -160 + 2 * number ).toString();
var number = "3";
var degrees = ( -160 + 2 * number ).toString();
console.log( number, degrees );
答案 12 :(得分:0)
所以,我有几种方法可以回答这个问题。它不是更短,但您的意图可能更明显,更直接的是switch
而不是if
s:
switch (number)
{
case "0":
degrees = "-160";
break;
case "1":
degrees = "-158";
break;
...
}
但是,对于这种类型的事情,查找表可能更清晰简洁:
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!"
有关详细信息,请参阅查找表部分here。
第三,number
和degrees
之间似乎存在直接的数学关系。也许只需编写一个函数来直接根据degrees
?
number