[SOLVED] What is wrong with this function and conditional statement output?

Issue

I’m going through a series of tutorials on JavaScript. One of them had me build a BMI calculator in JS, while checking my results. It also provided a solution and explained it out, too. This was it:

function bmiCalculator(weight, height) {
    var bmi = Math.round(weight / Math.pow(height, 2));
    console.log("Your BMI is " + bmi);
    return bmi;
}

var bmi = bmiCalculator(65, 1.8);
console.log(bmi);

The idea was that, if I pop in values into the var = bmi line for bmiCalculator, to get an output in the console. It gave me the 65 and 1.8 to try, telling me my output should be 20. Well, it worked (hooray) and I (thought?) I understaood what’s happening there.

A subsequent task asked me to modify it so that it incorporated conditional statements and output a sort of “you are under/normal/over-weight” message depending on the calculated BMI. It gives a starting code block of the following:

function bmiCalculator (weight, height) {
    return interpretation;
}

The problem is that there is no follow-up for this lesson. There isn’t a solution (or explanation of it). There’s a “check code” option but it just returns errors and I’m not sure what’s going wrong.

I had come up with this code block:

function bmiCalculator(weight, height) {
    var bmi = Math.round(weight / Math.pow(height, 2));
    return interpretation;
}

var interpretation = bmiCalculator(65, 1.8);

if (bmi <= 18.5) {
    alert("Your BMI is " + bmi + " so you are underweight.");
}
if (bmi > 18.5 && bmi <= 24.9) {
    alert("Your BMI is " + bmi + " so you have a normal weight.");
}
if (bmi > 24.9) {
    alert("Your BMI is " + bmi + " so you are overweight.");
}

It doesn’t give me a starting value to use, so I just used the 65 and 1.8 from the previous example. But its errors returned to me include:

Your solution is incorrect
bmiCalculator() should return underweight for 6, 2

Expected undefined to equal 'Your BMI is 15, so you are underweight.'.

Well, if I put 6,2 in that calculation, I’m not getting 15, I’m getting 2 (I ran it through the original bmiCalculator function as a test. So that doesn’t make sense to me.
Additionally, there’s another note alongside that message to me:

ReferenceError: Can't find variable: bmi

I thought that I define bmi within the function, though. Should it be defined in a different way?

Anyway, I’d like to get a solution to this task, since one isn’t provided, so I can understand what I am doing incorrectly before proceeding. Would anyone be able to tell me the correct solution, and what I did wrong, so I can learn from it?

Solution

You just mixed with variables, should be:

function bmiCalculator(weight, height) {
    var interpretation = Math.round(weight / Math.pow(height, 2));
    return interpretation;
}

var bmi = bmiCalculator(65, 1.8);

if (bmi <= 18.5) {
    alert("Your BMI is " + bmi + " so you are underweight.");
}
if (bmi > 18.5 && bmi <= 24.9) {
    alert("Your BMI is " + bmi + " so you have a normal weight.");
}
if (bmi > 24.9) {
    alert("Your BMI is " + bmi + " so you are overweight.");
}

Answered By – Vlad Goldman

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published.