切换案例失败,返回NaN

时间:2015-04-01 20:05:58

标签: javascript jquery

我有一个bmi计算器并将条件转换为switch语句以使其更清晰。但是,现在代码正在破坏,并且不会导致设置var结果,因此可以显示它。

我在这里缺少什么想法?

JS

 $('#calculatebutton').click(function () {
         var weight = parseInt($('#weight-lb').val()),
             heightInches = parseInt($('#height-ft').val()) + parseInt($('#height-in').val()),
             heightsqaured = (heightInches) * (heightInches),
             result = ((weight) / (heightsqaured) * 703);


              switch(result) {

                case (result < 16):
                     var rating = 'You are severely underweight';
                     break;


                case (result > 16) && (result < 18.5):
                     var rating = 'You are underweight';
                     break;


                case (result > 18.5) && (result < 25):
                     var rating = 'You are healthy';
                     break;


                case (result > 25) && (result < 30):
                     var rating = 'You are overweight';
                     break;


                case (result > 30) && (result < 35):
                     var rating = 'You are moderately obese';
                     break;


                case (result > 80):
                     var rating = 'This result seems unlikely, please check that the information you have entered is correct';
                     break;       
            }

            $('#result').html('Your BMI is ' + result.toFixed(1) + '. ' + rating + '.');

     });
JS小提琴 http://jsfiddle.net/o12xpy2s/

1 个答案:

答案 0 :(得分:1)

变化:

switch(result) {

...为:

switch(true) {

您的switch个案件都是truefalse条件。因此,将switch表达式switch(expression)设置为其中一个。在这里,您需要查找列出的true的{​​{1}}条件。