Calculation of body mass index (BMI)

My html code is as follows:

<!DOCTYPE html PUBLIC "-//WC3//DTD XHTML 1.0 Stric//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>JavaScript Aula 07</title>       
    <script type="text/javascript" src="js/aula07.js"></script>
    <style type="text/css">
    img, table { width:165px; }
    fieldset { width:140px; }
    label { display:block; float:left;}
    label, input {width:68px; margin:3px 0; }
    th, td {border:1px solid #ccc; font-size:0.8em;}
    </style>
</head>
<body>
    <img src="img/imc.png" alt="imc"/>
    <form id="formulario">
        <fieldset>
            <legend>Cálculo do IMC</legend>

            <label for="kilos">Quilos:</label>
            <input type="text" name="quilos"/>          

            <label for="metros">Metros:</label>
            <input type="text" name="metros"/>

            <label for="centimetros">Cm:</label>
            <input type="text" name="centimetros"/>

            <label for="imc">IMC:</label>
            <input type="text" name="imc" disabled="disabled"/>

            <a href="#" onclick="calcularIMC()">Calcular</a>
            </fieldset>
        </form>
    </body>
</html>

And JavaScript is:

calcularIMC = function (){
var formulario = document.getElementById("formulario");

var kilos = +formulario.kilos.value;    
var metros = +formulario.metros.value;  
var centimetros = +formulario.centimetros.value;

var altura = (metros*100 + centimetros)/100;    
var imc = kilos / (altura * altura);    
formulario.imc.value = imc.toFixed(2);
}

When using Notepad++, the HTML page appears not to be "calling" JavaScript, causing no results to appear in the IMC field. Grateful to anyone who can help.

Author: sscarvalho, 2014-08-01

1 answers

Hello, the script loads correctly. However, if you open your browser console (shortcut: F12 in most of them), you will see that a run error occurs, which prevents your function from finishing running.

With the console open, try clicking the Calculate button, and you'll have clues as to where your error is.

Just to clarify, it is necessary to exchange:

var kilos = +formulario.kilos.value;

By:

var kilos = +formulario.quilos.value;

Hope it helps! Hugs!

 1
Author: Rui Pimentel, 2014-08-01 20:28:54