how to fix Nan javascript

115 views Asked by At

when I start to write in the htPrice field, Nan appears in the ttcPrice field, how can i fix this?

calculatTTC() {
     const htPrice = parseFloat(this.htpriceTargets[0].value);
     const tvaPercent = parseFloat(this.tvaTargets[0].value);
     const ttcPrice= parseFloat(preTaxPrice + vatPercent);
    if (isNaN(htPrice) && isNaN(tvaPercent)) {
        this.ttcPriceTargets[0].value = 'the value is not correct';
    } else {
        this.ttcPriceTargets[0].value = ttcPrice;
    }

}
1

There are 1 answers

2
sashok1337 On

You should check that htPrice OR tvaPercent is NaN. And only if both are not - calculate TTC.

calculatTTC() {
    const htPrice = parseFloat(this.htpriceTargets[0].value);
    const tvaPercent = parseFloat(this.tvaTargets[0].value);
     
    if (isNaN(htPrice) || isNaN(tvaPercent)) {
        this.ttcPriceTargets[0].value = 'the value is not correct';
    } else {
        const ttcPrice= parseFloat(preTaxPrice + vatPercent);
        this.ttcPriceTargets[0].value = ttcPrice;
    }
}