for循环中的if语句出现意外结果

时间:2019-04-13 15:50:37

标签: javascript for-loop if-statement

我正在编写一些JavaScript(没什么花哨的-我是菜鸟),当我尝试将结果记录到控制台时,我得到了意外的结果。 else语句比正常情况更频繁地启动。例如,我看到如果i = 3且j = 3 =>(x1 + x2 = 6/100),则将调用else语句,这当然不应该作为6/100 <1进行。可能真的很密集,但是任何帮助将不胜感激。

谢谢。


EDIT//
Here are the logs I am getting:
Thanks for your comments, here is the log: 

0.023799999999999998
arbitrage.js:49 (3) [0.98, 0.01, 0.01]
arbitrage.js:48 0.034
arbitrage.js:49 (3) [0.97, 0.02, 0.01]
arbitrage.js:48 0.047599999999999996
arbitrage.js:49 (3) [0.96, 0.02, 0.02]   //point A
arbitrage.js:53 Hello
arbitrage.js:54 0.047599999999999996
arbitrage.js:55 (3) [0.96, 0.02, 0.02]   //point B
arbitrage.js:48 0.068
arbitrage.js:49 (3) [0.95, 0.03, 0.02]
arbitrage.js:48 0.07139999999999999
arbitrage.js:49 (3) [0.94, 0.03, 0.03] //point A
arbitrage.js:53 Hello
arbitrage.js:54 0.07139999999999999
arbitrage.js:55 (3) [0.94, 0.03, 0.03]  //point B
arbitrage.js:48 0.09519999999999999
arbitrage.js:49 (3) [0.9299999999999999, 0.04, 0.03] //point A
arbitrage.js:53 Hello
arbitrage.js:54 0.09519999999999999
arbitrage.js:55 (3) [0.9299999999999999, 0.04, 0.03]  //point B

If you look at Point A which has run through the normal if statement and then look at point B which has run through the else statement. The values for x2,x3 and hence x1 are the same. How can this run through both if and else? Also none of these should run through point B as  0.02+0.02 for example < 1.
//


export function arbitrageCalculator3Way(odds){

    let x1=0;
    let x2=0;
    let x3=0;

    let potentialMax=0;
    let maxMin=0;
    let holderCombo=[0,0,0];
    let arbReturn =0;



    for (var i=1;i<99;i++) //nearest percentage
    {
        for (var j=1;j<99;j++) 
        {

            x2 = i/100;
            x3 = j/100;

            if(x2+x3<1){

                potentialMax = (Math.min(odds[0][0]*(1-x2-x3),odds[0][1]*x2,odds[0][2]*x3));

                if(potentialMax>maxMin)
                {
                    maxMin=potentialMax;
                    holderCombo=[1-x2-x3,x2,x3];

                    console.log(maxMin);
                    console.log(holderCombo);
                    debugger;
                }



            }
            else{
                console.log("Hello");
                console.log(maxMin);
                    console.log(holderCombo);
                    debugger;
                break;
            }


        }

    }
    x1 = 1 -x2 - x3;
    arbReturn=(maxMin-1)*100;


   console.log(arbReturn);
    console.log(`${x1}   ${x2}   ${x3}`); ////hmmm error.
    debugger;
    return arbReturn;

}

0 个答案:

没有答案