为什么使用一个if条件可以运行,而另一个不行?

huangapple go评论82阅读模式
英文:

Can someone tell me why using one if condition works and the other doesn't?

问题

I understand you want a translation of the provided code and explanations. Here is the translated content:

我是一个初学者,需要理解 JavaScript 中的一个方法。

我想知道为什么使用 if (mark.calcBMI > john.calcBMI) 不起作用,但使用 if (mark.bmi > john.bmi) 起作用了?我认为它应该起作用,因为当我使用 console.log 输出 mark.calcBMI 和 john.calcBMI 时,它们都产生了正确的 BMI 结果,所以现在只需要告诉我哪个更高。

这是我的第一个代码,我创建了两个对象,每个对象都有一个函数来计算对象中每个人的 BMI。当我将其记录到控制台时,它不会查看 if 语句,而是直接进入 else 语句。无论我是否将 Mark 的 BMI 结果更改为计算高于 John 的,它仍然只进入 else 语句,不读取 if 条件。我已经加粗了 if 条件,它基本上是在告诉它查看每个方法的结果,并计算哪个 BMI 计算更大。附上我的 console.log 屏幕。

这是我的原始代码

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

console.log(mark.calcBMI());

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

console.log(john.calcBMI());

if (mark.calcBMI > john.calcBMI) {
    console.log(`${mark.firstName} ${mark.lastName} (${mark.bmi}) 高于 John 的`)
} else console.log(`${john.firstName} ${john.lastName} (${john.bmi}) 高于 Mark 的`)

在下面的代码中,我将其从 mark.calcBMI 更改为 mark.bmi,然后它开始查看我的 if 语句,并且给出了正确的结果,所以我知道它起作用了。下面是新的代码,与上面完全相同,但 if 条件已更改,已加粗。

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

console.log(mark.calcBMI());

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

console.log(john.calcBMI());

if (mark.bmi > john.bmi) {
    console.log(`${mark.firstName} ${mark.lastName} (${mark.bmi}) 高于 John 的`)
} else console.log(`${john.firstName} ${john.lastName} (${john.bmi}) 高于 Mark 的`)

希望这对你有所帮助!

英文:

I'm a beginner and i need help understanding a method in javascript.

I want to know why using - if (mark.calcBMI > john.calcBMI) did not work, but using if (mark.bmi > john.bmi) did? i would think it should work because when i console.log both mark.calcBMI and john.calcBMI they both produce the correct BMI result so now its just a case of telling me which is higher.

So here is my first code, i created two objects and each object has a function that calculates the BMI of each person in the object. when i log it to the console it doesn't look at the if statement and just goes straight to the else statement. No matter if i change Marks BMI result to calculate higher than Johns it still just goes to the else statement and doesn't read the if condition. I have bolded the if condition which is basically saying to look at the results of each methods and work out which BMI calculation is greater. Attached is my console.log screen.

Here is my original code

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

console.log(mark.calcBMI());

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


console.log(john.calcBMI());

if (**mark.calcBMI > john.calcBMI**) {
    console.log(`${mark.firstName} ${mark.lastName} (${mark.bmi}) is higher than John's`)
} else console.log(`${john.firstName} ${john.lastName} (${john.bmi}) is higher than Mark's`)type here

为什么使用一个if条件可以运行,而另一个不行?

below code, I then changed it from mark.calcBMI to mark.bmi and it stated to look at my if statement and was giving the right result so i knew it works. below is the new code which is the exact same as above but the if condition changed, bolded.

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

console.log(mark.calcBMI());

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


console.log(john.calcBMI());

if (**mark.bmi > john.bmi**) {
    console.log(`${mark.firstName} ${mark.lastName} (${mark.bmi}) is higher than John's`)
} else console.log(`${john.firstName} ${john.lastName} (${john.bmi}) is higher than Mark's`)type here

为什么使用一个if条件可以运行,而另一个不行?

Help is hugely appreciated!

答案1

得分: 1

你忘了调用你的 calcBMI() 方法来获取实际值以进行比较... 所以:

if (mark.calcBMI() > john.calcBMI()) {
   ...

但对于你的示例,我会使用类:

class Person {
    constructor(data){
      Object.assign(this, data);
    }
    get bmi() {
        return this.weight / (this.height * this.height);
    }
}

const mark = new Person({
    firstName: 'Mark',
    lastName: 'Miller',
    weight: 78,
    height: 1.69
});

const john = new Person({
    firstName: 'John',
    lastName: 'Smith',
    weight: 92,
    height: 1.95
});

console.log(mark.bmi, john.bmi, mark.bmi > john.bmi);

// sort to make it agnostic (and we don't logic when BMIs are equal)
const persons = [mark, john].sort((a, b) => b.bmi - a.bmi);

console.log(`${persons[0].firstName} ${persons[0].lastName} (${persons[0].bmi.toFixed(2)}) is higher than ${persons[1].firstName}'s`);

如果你想更复杂一点:

const mark = {
    firstName: 'Mark',
    lastName: 'Miller',
    weight: 78,
    height: 1.69
};

const john = {
    firstName: 'John',
    lastName: 'Smith',
    weight: 92,
    height: 1.95
};

const persons = [mark, john].map(person => Object.setPrototypeOf(person, {
    get bmi() {
        return this.weight / (this.height * this.height);
    }
}));

console.log(mark.bmi, john.bmi, mark.bmi > john.bmi);

// sort to make it agnostic (and we don't logic when BMIs are equal)
persons.sort((a, b) => b.bmi - a.bmi);

console.log(`${persons[0].firstName} ${persons[0].lastName} (${persons[0].bmi.toFixed(2)}) is higher than ${persons[1].firstName}'s`);
英文:

You forgot to call your calcBMI() methods to get actual values to compare... so:

 if (mark.calcBMI() > john.calcBMI() {
   ...

But for your example I would use classes:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

class Person {
    constructor(data){
      Object.assign(this, data);
    }
    get bmi() {
        return this.weight / (this.height * this.height);
    }

}

const mark = new Person({
    firstName: &#39;Mark&#39;,
    lastName: &#39;Miller&#39;,
    weight: 78,
    height: 1.69
});

const john = new Person({
    firstName: &#39;John&#39;,
    lastName: &#39;Smith&#39;,
    weight: 92,
    height: 1.95
});

console.log(mark.bmi, john.bmi, mark.bmi &gt; john.bmi);

// sort to make it agnostic (and we don&#39;t logic when BMIs are equal)
const persons = [mark, john].sort((a, b) =&gt; b.bmi - a.bmi);

console.log(`${persons[0].firstName} ${persons[0].lastName} (${persons[0].bmi.toFixed(2)}) is higher than ${persons[1].firstName}&#39;s`);

<!-- end snippet -->

If you want to go hardcore:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const mark = {
    firstName: &#39;Mark&#39;,
    lastName: &#39;Miller&#39;,
    weight: 78,
    height: 1.69
};

const john = {
    firstName: &#39;John&#39;,
    lastName: &#39;Smith&#39;,
    weight: 92,
    height: 1.95
};

const persons = [mark, john].map(person =&gt; Object.setPrototypeOf(person, {
    get bmi() {
        return this.weight / (this.height * this.height);
    }
}));

console.log(mark.bmi, john.bmi, mark.bmi &gt; john.bmi);

// sort to make it agnostic (and we don&#39;t logic when BMIs are equal)
persons.sort((a, b) =&gt; b.bmi - a.bmi);

console.log(`${persons[0].firstName} ${persons[0].lastName} (${persons[0].bmi.toFixed(2)}) is higher than ${persons[1].firstName}&#39;s`);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 02:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定