简体   繁体   中英

JavaScript IF statement not recognizing variable

I am having a problem with an if statement not recognizing a variables' value

The input is coming from a CSV file, saved to a variable and then passed into this function to check if it is true,

the checkusage console.logs are coming through with the value of "Y" but when ran it seems to keep skipping the checkusage if statement and executes the else statement.

VolumeChargeCalc = function(meterSize, checkusage){
        console.log("Check usage value passed to function is = " + checkusage );
        if(meterSize <= 20) {
            console.log("METER SIZE HAS PASSED");
                if(checkusage == "Y") {
                    console.log("Check usage has been found");
                    return  [0.8042,0.8042,0.6879,0.6627];
                } else {
                    console.log("no check usage found " + checkusage);
                    return  [2.1442,0.8042,0.6879,0.6627];
                } 

        } else if(meterSize == 999){
            return [0.03035,0,0,0];
        } 
        else {
            return [0.8042, 0.8042, 0.6879, 0.6627];
        }
    }

I have tried a few different ways and the all have the same outcome, any ideas would be appreciated.

Thanks @mplungjan,

if (checkusage.trim() == "Y")

solved the problem, I guess the CSV was passing spaces through as well.

  • Not sure how to mark your comment as the answer sorry

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