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之间的一个数字。
谢谢!
最佳答案
对于您的特定问题,您只需编写以下函数
var getDegrees = function(number){
return -160 + ((+number)*2);
}
叫它像波纹管
getDegrees("1") // will return -158
getDegrees("10") // will return -140
getDegrees(10) // will also return -158