# Fix BMI calculator

**URL:** <https://forum.freecodecamp.org/t/fix-bmi-calculator/123701>\
**Category:** I Built This\
**Created:** [June 5, 2017, 10:14am UTC](https://forum.freecodecamp.org/t/fix-bmi-calculator/123701 "2017-06-05T10:14:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fukenist](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fukenist/32/49023_2.png) [@fukenist](https://forum.freecodecamp.org/u/fukenist)\
**Post date:** [June 5, 2017, 10:14am UTC](https://forum.freecodecamp.org/t/fix-bmi-calculator/123701/1 "2017-06-05T10:14:36Z")

</div>

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](https://codepen.io/fukenist/pen/KqwNpy)

---

<div class="post-metadata">

**Author:** ![russelljoffe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/russelljoffe/32/56029_2.png) [@russelljoffe](https://forum.freecodecamp.org/u/russelljoffe)\
**Post date:** [June 5, 2017, 1:48pm UTC](https://forum.freecodecamp.org/t/fix-bmi-calculator/123701/2 "2017-06-05T13:48:28Z")

</div>

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)](https://developer.mozilla.org/en/docs/Web/API/HTMLInputElement). Currently, the **weight** and **height** variables are referencing the actual input element.

---

<div class="post-metadata">

**Author:** ![fukenist](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fukenist/32/49023_2.png) [@fukenist](https://forum.freecodecamp.org/u/fukenist)\
**Post date:** [June 6, 2017, 8:12am UTC](https://forum.freecodecamp.org/t/fix-bmi-calculator/123701/3 "2017-06-06T08:12:35Z")

</div>

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

> **[KqwNpy](https://codepen.io/fukenist/pen/KqwNpy)**
>
> ...

---

<div class="post-metadata">

**Author:** ![russelljoffe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/russelljoffe/32/56029_2.png) [@russelljoffe](https://forum.freecodecamp.org/u/russelljoffe)\
**Post date:** [June 6, 2017, 5:45pm UTC](https://forum.freecodecamp.org/t/fix-bmi-calculator/123701/4 "2017-06-06T17:45:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 2:33pm UTC](https://forum.freecodecamp.org/t/fix-bmi-calculator/123701/5 "2021-06-01T14:33:05Z")

</div>

Closed on June 1, 2021, 2:33pm UTC


