Fix BMI calculator

Hi,campers! I’m starting to learn JS and try to do simple BMI calculator. Despite of my inputs i got NaN answer. I guess that smth wrong with my variables. hlp me, please! https://codepen.io/fukenist/pen/KqwNpy

Hi

There are 2 issues. Firstly, you are retrieving the input elements using document.getElementById, but neither of your input’s actually have an id specified. If you want to use the name field, then you would need to use document.getElementsByName and filter the results.

Then, in your calculation, you will need to get the value of the input element, see MDN (HTMLInputElement). Currently, the weight and height variables are referencing the actual input element.

1 Like

I added “value” properties and used id’s. And i’m missing smth anyway.

Hi

The issue is that that the code in your tag is executed immediately as it is reached by the browser engine. So, you are retrieving the value of the input elements and storing them as they were at the start. In other words, the weight and height variables will both be empty at the start and remain that way.

You need to retrieve the value of the input elements after clicking the button, so that they have the current value, as updated by the DOM. The bmi function is executed on clicking the button, so that is when you should retrieve the value from the DOM element.