简体   繁体   中英

I don't know what's wrong with my coding. (javascript)

function calculateBmr(){
var weight = prompt("Enter weight in pounds");
var height = prompt("Enter height in inches");
var age = prompt("Enter age");
var bmr = 655 + (4.35 * weight) + (4.7 * height) - (4.7 * age);
alert("Your BMR is" bmr " calories.");
}
calculateBmr();

What's wrong with my code? It won't run.

alert("Your BMR is" bmr " calories.");

应该

alert("Your BMR is" + bmr +" calories.");

Try:

function calculateBmr(){
    var weight = parseFloat(prompt("Enter weight in pounds"));
    var height = parseFloat(prompt("Enter height in inches"));
    var age = parseFloat(prompt("Enter age"));
    var bmr = 655 + (4.35 * weight) + (4.7 * height) - (4.7 * age);
    alert("Your BMR is " + bmr.toString() + " calories.");
}
calculateBmr();

The parseFloat s will ensure that what the user enters is a number and + ought to be used for string concatenation.

Try this

alert("Your BMR is" +bmr+ " calories.");

you have to use the operator +

+bmr+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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