简体   繁体   中英

javascript adding a constant to a variable

I'm trying to validate user entered values as being within a range that is equal to a prior user entered number plus/minus constant numbers but the script seems to only recognize the lower bound of the range and the upper bound is always the prior user entered number plus any value less than one (aka user entered 55 so 55.999999 works but 56 turns red). Code:

if (BUNO.rawValue>=164865){ 
    if (Auto1Right.rawValue>=45 && Auto1Right.rawValue<=75)
    {
        var eleven = 11.00;
        var auto2min=Auto1Right.rawValue-eleven;
        var five = 5.70; 
        var auto2max=Auto1Right.rawValue+five;
        if (this.rawValue>=auto2min&&Man2Right.rawValue<=auto2max)
        {
            Man2Right.fillColor="0,255,0";
        }


    }
    else
    {
        Man2Right.fillColor="255,0,0";
    }

}

I think you might just have a small typo. You have two equal signs here

var auto2max==Auto1Right.rawValue+five;

It should probably look like this:

var auto2max = Auto1Right.rawValue + five;

apparently this can be avoided by changing the "+" symbol to a "-" symbol and changing your variable value to a negative number.

i have no idea why it won't use the "+" for addition without changing the variable to a string. According to everything i can find the "+" symbol should add both numbers and strings.

It sounds like the rawValue is being interpreted as a string, not a number. Have you tried casting your variables ?

if ( parseFloat(BUNO.rawValue) >= 164865 ){
    var a1r = parseFloat(Auto1Right.rawValue)
    if (a1r>=45 && a1r<=75)
    {
        var eleven = 11.00;
        var auto2min=Auto1Right.rawValue-eleven;
        var five = 5.70; 
        var auto2max = a1r + five;

        var thisRV = parseFloat(this.rawValue) ;
        var m2RV = parseFloat(Man2Right.rawValue) ;
        if (thisRV>=auto2min && m2RV<=auto2max)
        {
            Man2Right.fillColor="0,255,0";
        }


    }
    else
    {
        Man2Right.fillColor="255,0,0";
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM