我正在寻找使用我在javascript中制作的公式的修复程序

时间:2019-04-10 22:11:21

标签: javascript html css

基本上,我有一个网站正在制作,它是从通用回形针衍生而来的,称为橡皮筋。当然,在此过程中,您需要找到一个不错的公式来计算购买橡皮筋的人数与公共利益的增长。这样,公众利益越高,就会有更多的人购买您的橡皮筋。

我有一个正在使用的公式,但是它非常主观且有缺陷,而且如果您的公共利益超过800%,它将不起作用。以下是我以100ms间隔使用的公式。

 setInterval (function() {
       if (publicinterest < 100) {
         if (Math.random() < publicinterest / 200) {
           if (Math.random() < publicinterest / 200) {
             if (unsoldrubberbands > 0) {
               money += sellingprice; 
               publicinterest = publicinterestdecider / 
sellingprice;  
                 unsoldrubberbands -= 1; 
                 document.getElementById('moneytext').innerHTML = 
'Money: $' + money.toFixed(2); 
               document.getElementById('inventory').innerHTML = 
'Inventory: ' + unsoldrubberbands;
               document.getElementById('publicinterest').innerHTML = 
"Public Interest " + publicinterest.toFixed(1) + '%';
           }
         }
       }
     }
   if (publicinterest < 200) {
       if (Math.random() * 2 < publicinterest / 190) {
         if (Math.random() * 2 < publicinterest / 190) {
           if (unsoldrubberbands > 0) {
             money += sellingprice; 
             publicinterest = publicinterestdecider / sellingprice;
             unsoldrubberbands -= 1; 
             document.getElementById('moneytext').innerHTML = 'Money: $' + money.toFixed(2); 
             document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
             document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
         }
       }
     }
   }

   if (publicinterest < 300) {
       if (Math.random() * 3 < publicinterest / 180) {
         if (Math.random() * 3 < publicinterest / 180) {
           if (unsoldrubberbands > 0) {
             money += sellingprice; 
             publicinterest = publicinterestdecider / sellingprice;
             unsoldrubberbands -= 1; 
             document.getElementById('moneytext').innerHTML = 'Money: $' + money.toFixed(2); 
             document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
             document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
         }
       }
     }
   }

   if (publicinterest < 400) {
       if (Math.random() * 4 < publicinterest / 170) {
         if (Math.random() * 4 < publicinterest / 170) {
           if (unsoldrubberbands > 0) {
             money += sellingprice; 
             publicinterest = publicinterestdecider / sellingprice;
             unsoldrubberbands -= 1; 
             document.getElementById('moneytext').innerHTML = 'Money: $' + money.toFixed(2); 
             document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
             document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
         }
       }
     }
   }

   if (publicinterest < 500) {
       if (Math.random() * 5 < publicinterest / 160) {
         if (Math.random() * 5 < publicinterest / 160) {
           if (unsoldrubberbands > 0) {
             money += sellingprice; 
             publicinterest = publicinterestdecider / sellingprice;
             unsoldrubberbands -= 1; 
             document.getElementById('moneytext').innerHTML = 'Money: $' + money.toFixed(2); 
             document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
             document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
         }
       }
     }
   }

   if (publicinterest < 600) {
       if (Math.random() * 6 < publicinterest / 150) {
         if (Math.random() * 6 < publicinterest / 150) {
           if (unsoldrubberbands > 0) {
             money += sellingprice; 
             publicinterest = publicinterestdecider / sellingprice;
             unsoldrubberbands -= 1; 
             document.getElementById('moneytext').innerHTML = 'Money: $' + money.toFixed(2); 
             document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
             document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
         }
       }
     }
   }

   if (publicinterest < 700) {
       if (Math.random() * 7 < publicinterest / 140) {
         if (Math.random() * 7 < publicinterest / 140) {
           if (unsoldrubberbands > 0) {
             money += sellingprice; 
             publicinterest = publicinterestdecider / sellingprice;
             unsoldrubberbands -= 1; 
             document.getElementById('moneytext').innerHTML = 'Money: $' + money.toFixed(2); 
             document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
             document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
         }
       }
     }
   }

   if (publicinterest < 800) {
       if (Math.random() * 8 < publicinterest / 130) {
         if (Math.random() * 8 < publicinterest / 130) {

           if (unsoldrubberbands > 0) {
             money += sellingprice; 
             publicinterest = publicinterestdecider / sellingprice;
             unsoldrubberbands -= 1; 
             document.getElementById('moneytext').innerHTML = 'Money: $' +   money.toFixed(2); 
             document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
             document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
         }
       }
     }
   }
     //console.log('sellingpriceint', sellingprice);
     //console.log('publicinterestint', publicinterest);
     //console.log('rubberbandsint', rubberbands)


 },100)

此代码描述了4个变量。公众利益是指要根据销售量提高销售量的事件。售价是您出售橡皮筋的金额,因此,价格越低,公众利益就越高。从根本上说,公共利益决定者是利润以及我如何通过升级来增加公共利益。未售出的橡胶带只是库存。金钱是不言而喻的。因此,我真正要寻找的是一个更好的公式,该公式将根据公共利益成倍地起作用,并且无论公共利益是什么都行得通。 (请记住,公共利益本身只是一个数字,但在显示屏上我在其旁边添加了一个百分号)

1 个答案:

答案 0 :(得分:1)

这是使大部分代码可重复使用的一种方法:

setInterval (function() {
    if (publicinterest < 100) {
        if (Math.random() < publicinterest / 200) {
            if (Math.random() < publicinterest / 200) {
                if (unsoldrubberbands > 0) {
                    money += sellingprice; 
                    publicinterest = publicinterestdecider / sellingprice;  
                    unsoldrubberbands -= 1; 
                    document.getElementById('moneytext').innerHTML = 'Money: $' + money.toFixed(2); 
                    document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
                    document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
                }
            }
        }
    }

    for(var loop = 0; loop < 7; loop++){
        if(
            publicinterest < loop * 100 + 200
            &&
            Math.random() + (loop + 2) < publicinterest / (190 - 10 * loop)
            &&
            Math.random() + (loop + 2) < publicinterest / (190 - 10 * loop)
            &&
            unsoldrubberbands > 0
        ) {
             money += sellingprice; 
             publicinterest = publicinterestdecider / sellingprice;
             unsoldrubberbands -= 1; 
             document.getElementById('moneytext').innerHTML = 'Money: $' + money.toFixed(2); 
             document.getElementById('inventory').innerHTML = 'Inventory: ' + unsoldrubberbands;
             document.getElementById('publicinterest').innerHTML = "Public Interest " + publicinterest.toFixed(1) + '%';
        }
    }

},100)

我不确定您是否希望每个时间间隔运行7次代码,但是如果是这样,这样做就可以了。