function bmrs($weight=0,$sex=1,$age){
switch ($age) {
case $age>=0 and $age<3:
$x=array(1=>array(60.9,-54),2=>array(61.0,-51));
$y=1;
break;
case $age>=3 and $age<10:
$x=array(1=>array(22.7,495),2=>array(22.5,499));
$y=1;
break;
case $age>=10 and $age<18:
$x=array(1=>array(17.5,651),2=>array(12.2,746));
$y=1;
break;
case $age>=10 and $age<18:
$x=array(1=>array(17.5,651),2=>array(12.2,746));
$y=0.95;
break;
case $age>=18 and $age<30:
$x=array(1=>array(15.3,679),2=>array(14.7,496));
$y=0.95;
break;
case $age>=30 and $age<60:
$x=array(1=>array(11.6,879),2=>array(8.7,829));
$y=0.95;
break;
case $age>=60:
$x=array(1=>array(13.5,487),2=>array(10.5,596));
$y=0.95;
break;
default:
break;
}
return ceil(($x[$sex][0]*$weight+$x[$sex][1])*$y); //对浮点数进行四舍五入
}
自带参数这几个,前面二个是默认值吗?$weight=0,$sex=1,$age
这二个代码,修改为JS,遇到了难度:
case $age>=0 and $age<3:
$x=array(1=>array(60.9,-54),2=>array(61.0,-51));
哪位帮个忙,把它修改为JavaScript的,谢谢。
------解决方案--------------------
- JScript code
function bmrs(weight,sex,age){ var x=new Array(2),y=null; switch (true) { case age>=0 && age<3: x[1]=new Array(60.9,-54),x[2]=new Array(61.0,-51); y=1; break; case age>=3 && age<10: x[1]=new Array(22.7,495),x[2]=new Array(22.5,499); y=1; break; case age>=10 && age<18: x[1]=new Array(17.5,651),x[2]=new Array(12.2,746); y=1; break; case age>=10 && age<18: x[1]=new Array(17.5,651),x[2]=new Array(12.2,746); y=0.95; break; case age>=18 && age<30: x[1]=new Array(15.3,679),x[2]=new Array(14.7,496); y=0.95; break; case age>=30 && age<60: x[1]=new Array(11.6,879),x[2]=new Array(8.7,829); y=0.95; break; case age>=60: x[1]=new Array(13.5,487),x[2]=new Array(10.5,596); y=0.95; break; default: break; } return Math.ceil((x[1][0]*weight+x[1][1])*y); }