jeudi 2 août 2018

If statement to check if Variable isNan, then alert if true

There are a few questions asked similar to this, but I may have the wrong idea and cannot get it to work. My JS is still new, so this might be a simple question.

I have created a loan calculator, when the fields are empty it returns "NaN" and I am trying to display a message when it is ran on empty fields. Then, run as it does when the fields are filled.

TIA. Comments on the calculator's code are appreciated too.

function calc() {
    //Collecting all the amounts
    var amount = document.getElementById("amount").value;
    var time = document.getElementById("time").value;
    var interest = document.getElementById("interest").value;
    //breaking down equation into steps
    var a = (parseInt(amount.replace(/,/g, ''), 10));
    var t = -Math.abs((parseInt(time.replace(/,/g, ''), 10)) * 12);
    var i = ((parseInt(interest.replace(/,/g, ''), 10)) / 100) / 12;
    var x = Math.pow((1 + i), t);
    var result = (i / (1 - x)) * a;

        if (result !== null) {
            alert("Fill in all Fields")
        }else{
            document.getElementById("calc").innerHTML = "$" + result.toFixed(2) + " a month";
        }
}

Aucun commentaire:

Enregistrer un commentaire