0

I'm trying to make an IMC calculator that shows an alert depending your IMC value. But when I try to make a conditional for show an alert depending the body-status it doesn't come

var k =
  { nombre       : 'Antonio'
  , peso         : 85
  , altura       : 1.80
  , masacorporal : function ()
    {
    var r = this.peso / (this.altura*this.altura);
    return Math.round(r*100)/100;
    }
  }
function f()
  {
  k.peso   = document.getElementById("dato1").value;
  k.altura = document.getElementById("dato2").value;
  
  document.getElementById("resultado").innerHTML = k.masacorporal();
  
  var error = 0;
  if (k.peso == '' && error == 0)
    {
    alert("El valor del peso es obligatorio");
    k.peso.focus();
    error=1;
    }
  if (k.altura=='' && error==0)
    {
    alert("El valor de la altura es obligatorio");
    k.altura.focus();
    error=1;
    }
  if (!/^([0-9])*$/.test(k.peso))
    {
    alert("El valor " + k.peso + " no es un número");
    m1.focus();
    error=1;
    }
  if (error==0)
    {
    f()
    }
  }
function m()
  {
  if (k.masacorporal < 18.5) 
    { 
    alert("Bad healt status"); 
    }
  }
PESO (kg): <input type="text" id="dato1" onkeypress=""/>
<br/>
Altura (metros): <input type="text" id="dato2"/>
<br/>
<input   type="button" value="Calcular" onclick="f()"/>
<br/>
IMC:<span id="resultado"> </span>

Thats the full code if u want to try it, for me the alert doesnt show even if i have corrected the last part

8
  • PMI but what's IMC? Commented Mar 12, 2021 at 22:55
  • @j08691 BMI (body mass index) in Spanish. Commented Mar 12, 2021 at 22:56
  • 2
    masacorporal is a function off of k Commented Mar 12, 2021 at 22:58
  • 1
    Do you have a script tag in the middle of your code? Commented Mar 12, 2021 at 22:59
  • 1
    yes, im going to post my full code Commented Mar 12, 2021 at 23:00

2 Answers 2

1

You are trying to access method inside object directly. You should add object name before method.

function m() {
    if (k.masacorporal < 18.5){alert("Bad healt status");}
}
Sign up to request clarification or add additional context in comments.

3 Comments

its a function.
It is perfectly fine to call functions that are defined as object properties methods.
Joselito, where do you call m?
0

You need to write masocorporal as k.masacorporal().

It has to be prefixed with k. because it's a property of k, and it has to have parentheses to execute it because it's function. Otherwise you're checking if the function is smaller than 18.5 which makes no sense; you want to compare the value the function returns.

var k = {
  nombre: "Antonio",
  peso: 0,
  altura: 1.80,
  masacorporal: function () {
    var r = this.peso / (this.altura * this.altura);
    return Math.round(r * 100) / 100;
  }
};

function m() {
  if (k.masacorporal() < 18.5) { alert("Bad health status"); }
}

m();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.