价值只改变一次,为什么

时间:2019-01-17 18:20:45

标签: javascript

每次在9或10处生成随机数时,priceCur需要更新为新值。 这似乎没有发生,我也不知道为什么。

var companyOne = {
    industry:"tech",
    cName:"",
    totalShares:100000,
    priceStart:10,
    priceCur:10
};

var randomGenerated = Math.floor((Math.random() * 11) + 0); // 0 - 10
console.log(randomGenerated);

if(randomGenerated >= 9){
    companyOne.priceCur = companyOne.priceCur * (1 + 0.050); //5% increase
    console.log('current_share', companyOne.priceCur)

}

1 个答案:

答案 0 :(得分:1)

目前,它只运行一次,因为您只分配和记录一次值。为了使其多次更改,您需要添加条件语句或事件侦听器等,以便它可以多次运行。这是一个示例(无限循环):

while (true) {

    var companyOne = {
        industry:"tech",
        cName:"",
        totalShares:100000,
        priceStart:10,
        priceCur:10
    };

    var randomGenerated = Math.floor((Math.random() * 11) + 0); // 0 - 10
    console.log(randomGenerated);

    if(randomGenerated >= 9) {
        companyOne.priceCur = companyOne.priceCur * (1 + 0.050); //5% increase
        console.log('current_share', companyOne.priceCur)

    }

}