Ao setar os valores do imc no objeto pacient, tentei pegar os valores de peso e alura com o "this".
var btnAddPacient = document.querySelector("#adicionar-paciente");
btnAddPacient.addEventListener("click", function(event){
event.preventDefault();
var form = document.querySelector("#form-adiciona");
var pacient = getPacientForm(form);
var pacientTr = createTrElement(pacient);
var table = document.querySelector("#tabela-pacientes");
table.appendChild(pacientTr);
});
function getPacientForm(form){
var pacient = {
name : form.nome.value,
weight : form.peso.value,
height : form.altura.value,
fat : form.gordura.value,
imc : calculaImc(this.weight, this.height)
}
return pacient;
}
function createTrElement(pacient){
var pacientTr = document.createElement("tr");
pacientTr.appendChild(createTdElement(pacient.name));
pacientTr.appendChild(createTdElement(pacient.weight));
pacientTr.appendChild(createTdElement(pacient.height));
pacientTr.appendChild(createTdElement(pacient.fat));
pacientTr.appendChild(createTdElement(pacient.imc));
return pacientTr;
}
function createTdElement(data){
var td = document.createElement("td");
td.textContent = data;
return td;
}
Porém, o valor que aparece no formulário após os dados serem preenchidos é NaN. Por que não deu certo?