1

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.

4
  • how you are calling VolumeChargeCalc() ? Commented Aug 24, 2016 at 11:44
  • Is meterSize <= 20 (so that it reaches if checkusage) ? Does if(checkusage.indexOf("Y") > -1) work? Commented Aug 24, 2016 at 11:45
  • 2
    Perhaps if (checkusage.trim() == "Y") Commented Aug 24, 2016 at 11:45
  • It seems to work fine: jsfiddle.net/g15wdexj Commented Aug 24, 2016 at 11:45

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.