I am building a BMI-calculator, (you put your height and weight and it calculates it) but I am having an issue where an if statement only outputs an error message variable to the div, but won’t output the correct one, even when there are the correct inputs.
I have attempted checking if the variables in the if statement are empty instead of checking for valid input.
Outputting the correct variable itself outside of the if statement works fine, only the if statement has an issue.
The console shows no problems.
let output = 'Your BMI is ' + shortened + '.';
let corrNum = 'Put correct numbers.';
if( weight !== '' && height !== '') {
document.getElementById("outputField").innerHTML = output;
} else {
document.getElementById("outputField").innerHTML = corrNum;
}
If there is inputs in both inputboxes, it should output ‘output’.
If one or both inputs are missing, it should output ‘corrNum’.
I would really appreciate any help.
2
Answers
Agree with the first commenter; you need to ensure that you are getting your inputs correctly.
Assuming you have two text boxes, one for height and one for weight:
Then you would have your code to check the values and display the messages. Please note that this is using jQuery to pull these values; there is no indication of what frameworks you are using.
You can use the built-in validation provided by the form to enforce the requiredness of the fields.