2014-07-13 90 views
0

我正在比较同一对象的两个属性来计算出哪一个较大,如果其中一个较大,则会将另一个属性设置为True。否则它将其设置为false。无法确定某个对象的某个属性是否大于该对象的另一个属性

这里是我的功能:

country.prototype.cure = function(){ 
    for (var i = 0; i<this.diseases.length; i++) 
    { 
    console.log(this.health); 
    console.log(this.diseases[i].cureLevel); 
    if (this.heatlh >= this.diseases[i].cureLevel) 
    { 
    this.diseases[i].cured = true; 
    createDiseaseTable(); 
    } 
    else 
    { 
     this.diseases[i].cured = false; 
    } 
} 
} 

注:this.health = 39000000和this.diseases [I] .cureLevel = 2500000

的问题是,每当我运行的功能,尽管这。健康是较大的,它会永远错过了,如果和直行到别的...

+1

你有没有真的在你的实际代码中拼写“heatlh”而不是“健康”? – Pointy

+0

是的,你是对的我在这个功能上拼错了它......这是漫长的一天..谢谢你们! – ScottRedrup

回答

0

试试这个:

country.prototype.cure = function(){ 
    for (var i = 0; i<this.diseases.length; i++) 
    { 
    var a=parseInt(this.health); 
    var b=parseInt(this.diseases[i].cureLevel); 
    if (a >= b) 
    { 
    this.diseases[i].cured = true; 
    createDiseaseTable(); 
    } 
    else 
    { 
     this.diseases[i].cured = false; 
    } 
} 
} 
+0

只有当属性值是字符串而不是数字时'parseInt'才有用;没有迹象表明在OP中就是这种情况,尽管这当然可能是事实。 – Pointy

+0

我没有看到任何其他可能的错误。我可能是错的,这就是为什么它尝试这个。 – nsthethunderbolt

相关问题