Operation not working when using number properties (returns NaN)

杀马特。学长 韩版系。学妹 提交于 2019-12-13 07:06:44

问题


What's the problem in using number properties? I'm trying to do a simple calculation involving numbers and it returns NaN.

function test () {
    var that = this;

    this.usersCount = 2;
    this.totalSeeds = 10;

    this.test2 = function () {
        console.log(2/10*100); // 20
        console.log(that.usersCount * that.totalSeeds); // 20
        var percentUsersCount = that.usersCount / that.totalseeds * 100; // also tried parseInt() and Number()
        console.log(percentUsersCount); // NaN -- WHY !?!
    }
}

var test = new test();

test.test2();

var test1 = 2;
var test2 = 10;
var percent = test1 / test2 * 100;
console.log(percent); // 20

Why is percentUsersCount NaN?

http://jsfiddle.net/ht2rv1ea/

Edit: https://www.youtube.com/watch?v=yI6VXlNRrI0


回答1:


You misspelled the name of your variable. Use that.totalSeeds instead of that.totalseeds.




回答2:


You have a typo:

  var percentUsersCount = that.usersCount / that.totalseeds * 100;

Change to

  var percentUsersCount = that.usersCount / that.totalSeeds * 100;


来源:https://stackoverflow.com/questions/32905080/operation-not-working-when-using-number-properties-returns-nan

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!