Hi the problem is following:
I define:
var total = 0;
function add(a){
total+=a;
var float_num = total.toFixed(2);
return float_num;
}
The JS give me an error said Uncaught TypeError total.toFixed is not a function
I don't get it. the total I declare is not a number type?
I think the easiest way to prevent the error from happening is to always parse the parameter as number:
var total = 0;
function add(a){
total+=a;
var float_num = Number(total).toFixed(2);
return float_num;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With