Home > Software design >  if/else statement isnt updating when given different values
if/else statement isnt updating when given different values

Time:11-06

const mark = {
    fullName: 'Mark Miller',
    mass: 78,
    height: 1.69,
    calcBMI: function () {
        this.bmi = this.mass / (this.height * this.height);
        return this.bmi
    }

};

const john = {
    fullName: 'John Smith',
    mass: 92,
    height: 1.95,
  calcBMI: function () {
        this.bmi = this.mass / (this.height * this.height);
        return this.bmi
    }
    
};


mark.bmi < john.bmi ? console.log(`${john.fullName}'s BMI ${john.calcBMI()} is greater than ${mark.fullName}'s BMI of ${mark.calcBMI()}`) 
: console.log(`${mark.fullName}'s BMI ${mark.calcBMI()} is greater than ${john.fullName}'s BMI of ${john.calcBMI()}`)

so this code DOES produce the right log, except if i change johns height or weight the if/else doesnt update and log a different result and im confused as to why

thanks!

CodePudding user response:

You could have just printed the bmi values before and after invocation on calcBmi member function. And in order to get desired bmi value use getter method.

const mark = {
    fullName: 'Mark Miller',
    mass: 78,
    height: 1.69,
    calcBMI: function () {
        this.bmi = this.mass / (this.height * this.height);
        return this.bmi
    }

};

const john = {
    fullName: 'John Smith',
    mass: 92,
    height: 1.95,
    calcBMI: function () {
        this.bmi = this.mass / (this.height * this.height);
        return this.bmi
    }
    
};

// You can see difference, BMI field is not created yet, but post invocation on calcBmi, bmi is giving result.
console.log(mark.bmi)
console.log(john.bmi)
console.log(mark.calcBMI())
console.log(john.calcBMI())
console.log(mark.bmi)
console.log(john.bmi)
//
mark.calcBMI() < john.calcBMI() ? console.log(`${john.fullName}'s BMI ${john.calcBMI()} is greater than ${mark.fullName}'s BMI of ${mark.calcBMI()}`) 
: console.log(`${mark.fullName}'s BMI ${mark.calcBMI()} is greater than ${john.fullName}'s BMI of ${john.calcBMI()}`)


Getter method example -

const alan = {
    fullName: 'Alan Miller',
    mass: 78,
    height: 1.69,
    get bmi() {
        return this.mass / (this.height * this.height);
    }

};
console.log(alan.bmi)

CodePudding user response:

Your users' bmis were showing up as undefined because of syntax problems. You want to use function objects for your users, and take calcBMI() out of the objects, because you only want to define it once:

const mark = new function(){
    this.name = 'Mark Miller',
    this.mass = 78,
    this.height = 1.69,
    this.bmi = calcBMI(this.mass, this.height)
};

const john = new function(){
    this.name = 'John Smith',
    this.mass = 92,
    this.height = 1.95,
    this.bmi =  calcBMI(this.mass, this.height) 
};

function calcBMI(m, h) {
  return m / (h * h)
}

mark.bmi < john.bmi ? 
  console.log(`${john.name}'s BMI ${john.bmi} is greater than ${mark.name}'s BMI of ${mark.bmi}`) 
: console.log(`${mark.name}'s BMI ${mark.bmi} is greater than ${john.name}'s BMI of ${john.bmi}`)

  • Related