简体   繁体   English

Javascript - SyntaxError: Illegal return statement 如何解决?

[英]Javascript - SyntaxError: Illegal return statement How to fix?

I can't understand where did I made a mistake, but I'm getting the following errors in the code below :我不明白我在哪里犯了错误,但我在下面的代码中收到以下错误:

Uncaught SyntaxError: Illegal return statement
SyntaxError: return not in function

Code:代码:

function bmiCalculator(weight, height) {
    var bmi = weight / Math.pow(height, 2);
    return Math.floor(bmi);
}
var bmi = bmiCalculator(65, 1.8);
if (bmi < 18.5) {
    return 'Your BMI is ' + bmi + ' so you are underweight';
}
if (bmi > 18.5 && bmi < 24.9) {
    return 'Your BMI is ' + bmi + ' so you have a normal weight';
} else {
    return 'Your BMI is ' + bmi + ' so you are overweight';
}

I'm a total newbie so i appreciate an explanation.我是一个完全的新手,所以我很感激解释。

You cannot use a function with the return parameter.您不能使用带有返回参数的函数。 First apply math.floor (bmi) under a new variable name.首先在新变量名下应用 math.floor (bmi)。 then return this variable.然后返回这个变量。 Like This:像这样:

function bmiCalculator(weight, height) {
var bmi = weight / Math.pow(height, 2);
var newBmi = Math.floor(bmi);
return newBmi;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM