0

In the below program i am using 3 if conditions, but the if statement in the middle doesn't works and last if statement works fine,when I change third if statement to second statement again second statement is not working properly. Third if statement works fine

function calculate() {
    var quantity = document.getElementById("quantity").value;
    var price = document.getElementById("price").value;
    var discount = document.getElementById("discount").value;
    var tax = document.getElementById("tax").value;

    if((quantity && price ) != null) {
        amount = quantity * price;
        document.getElementById("amount").value = amount;
    } else {
        alert("price & tax required");
    }

    if(discount != null) {
        var discount_amount = (quantity*price*discount)/100;
        amount = (quantity*price) - discount_amount;
        document.getElementById("amount").value = amount;
    } else {
        document.getElementById("amount").value = quantity * price;
    }

    if(tax != null) {
        var tax_amount = (quantity*price*tax)/100;
        amount = (quantity*price) + tax_amount;
        document.getElementById("amount").value = amount;
    } else {
        document.getElementById("amount").value = quantity * price;
    }
}
Edwin Thomas
  • 466
  • 4
  • 16
  • 7
    Whats the problem? like, what is not working. What should it do, what isn't it doing? what does the js-console say ? – flx Apr 16 '18 at 12:01
  • 3
    Can you put your HTML in your question too? – Jari Rengeling Apr 16 '18 at 12:01
  • try document.write(discount) to see the value of the discount to make sure that isn't null – StuiterSlurf Apr 16 '18 at 12:02
  • 4
    @StuiterSlurf please don't suggest `document.write()` as that will confuse the issue when it wipes the contents of the page. `console.log()` is perfect for quick debugging. I'd even prefer an `alert()` over `document.write()`!!! – Reinstate Monica Cellio Apr 16 '18 at 12:05
  • Like Nina pointed out a string in Javascript is a string, a null is a different type. Empty strings equal `""`, not null. But if you do want empty strings to be null, you could also do -> `var quantity = document.getElementById("quantity").value || null` – Keith Apr 16 '18 at 12:11
  • @Keith Doh - my bad :) – Reinstate Monica Cellio Apr 16 '18 at 12:22

4 Answers4

2

input.value returns a string, which is never null, but it can be an empty string, like ''.

Empty strings a converted to zero in numerical context. If that is not wanted, you need an explicit check, like

if (input.value === '') {
    return;
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0
if(discount != null)

discount will be empty string and not null check for:

 if(discount !== "")
b3nc1
  • 106
  • 7
0

You don't need to explicitly check for null in any of your if tests, as zero values as well as undefioned

if (quantity && price) {
   // only want to come in here for non-zero values of both
}

if (discount) {
   // only interested in the discount for non-zero values
}

if (tax) {
  // only interested in non-zero values of tax
}
vogomatix
  • 4,856
  • 2
  • 23
  • 46
0

if(discount != null)

lets assume discount variable is empty string. What do you thnik about result ?

if(""!=null)

It will be true What if your string comes fullfiled with some data

if("someData"!=null)

It will be trueagain.

I do not blame you. Javascript has some magical act in these kinds of situation.

In javascript, there is a lot of logic operation that can represent as false for if condition. Below code, all if statements return false

if(""){//code here}
if(null){//code here}
if(0){//code here}

A developer should not compare two different types like null and string. Just in case, I advise you to avoid double equal sign ==. Use triple === it is type-sensitive . see here

Ozan Ertürk
  • 465
  • 5
  • 17