Program that calculates BMI

Asked

Viewed 90 times

-1

I am creating a Javascript program that calculates BMI, there are fixed patients who already come with their data filled and I am putting the option to add more patients. The problem is when a new patient is created because it is not filling a part of the table "classification" I tried to create a function with "if" but this returning "Undefined", I don’t know what else to do.

The function is inside a for

function classe () {
    if (imc) {

        var tdClass = paciente.querySelector(".info-class")
        tdClass.textContent = "Teste";
    }

    return;
}

var executa = classe();

Here I created another solution to get the patient’s data


    var paciente = {

        nome: form.nome.value,
        peso: form.peso.value,
        altura: form.altura.value,
        gordura: form.gordura.value,
        imc: calculaImc(form.peso.value, form.altura.value),
        classificaocao: classe()
        
    }
    
    return paciente;
};

E essa outra para adicionar os novos paciente a tabela

    function montaTr (paciente){

    var pacienteTr = document.createElement("tr");
    pacienteTr.classList.add("paciente");
    
    pacienteTr.appendChild(montaTd(paciente.nome, "info-nome"));
    pacienteTr.appendChild(montaTd(paciente.peso, "info-peso"));
    pacienteTr.appendChild(montaTd(paciente.altura,"info-altura"));
    pacienteTr.appendChild(montaTd(paciente.gordura, "info-gordura"));
    pacienteTr.appendChild(montaTd(paciente.imc, "info-imc"));
    pacienteTr.appendChild(montaTd(paciente.classificaocao, "info-class"));

    return pacienteTr;
};
  • 1

    Puts all the code, help. Including the html part.

1 answer

0

For the classification to be according to BMI:

In var patient classificacao: classe(imc)

The loop for

    var pacientes = document.querySelectorAll(".paciente");

    for (var i = 0; i < pacientes.length; i++) {
                
        var paciente = pacientes[i];
        
        var tdPeso = paciente.querySelector(".info-peso");
        var peso = tdPeso.textContent;
        
        var tdAltura = paciente.querySelector(".info-altura");
        var altura = tdAltura.textContent;
        
        var tdImc = paciente.querySelector(".info-imc"); 
         
        tdImc.textContent = calculaImc(peso,altura);
                        
        var tdClass = paciente.querySelector(".info-class");
                               
        tdClass.textContent = classe(imc); 
                            
    }

The class function

function classe (valorIMC){

        if (valorIMC<18.5){
            classificacao="Magreza";
        }else if (valorIMC<24.9){
            classificacao="Normal";
        }else if (valorIMC<29.9){
            classificacao="Sobrepeso";
        }else{
            classificacao="Obesidade";
        }

    return classificacao;
}
  • friend actually I do not want the classification to be provided by the user and yes filled automatically according to the imc, for example imc 18.5 - 24.9 proper classification

Browser other questions tagged

You are not signed in. Login or sign up in order to post.