I am creating a loan calculator in which the user must first select the type of calculation he wants. Then after filling the value, the answer's textnode updates. But when I am multiplying two values, it gives me NaN. I have tried console log both values separately and both are returning true values. I don't know what I am doing wrong.
class Calculation{
constructor(a,b){
this.a = a;
this.b = b;
}
multiply(a,b){
return a*b;
} }
document.querySelector('#cal-form').addEventListener("submit", function(e){
const loaninput = parseInt(document.querySelector('#loan_val').value);
const interestinput = parseInt(document.querySelector('#interest_val').value);
const cal = new Calculation;
const result = cal.multiply(loaninput,interestinput);
console.log(result);
e.preventDefault();
});
it giving right number on my side:
class calculator{
constructor(a,b){
this.a = a
this.b = b
}
muliply(a,b){
return a*b
}
}
let a = new calculator()
console.log(a.muliply(2,3));
Any number multiple with NaN give the result is NaN.
You should log your loan_val and interest_val and ensure that these values can be convert to valid integer value by parseInt.
I think one of your input after parseInt return NaN.
should be this:
const cal = new Calculation();
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes