-3

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;
    }

}
learn
  • 1
  • 2
  • Is `this.htpriceTargets[0].value` defined? – User456 Jul 03 '22 at 12:46
  • The `if` condition should be an OR, not AND, but it is just easier to make the check on the result: `if (isNaN(ttcPrice))`. Also, there's no need to call `parseFloat` on numbers. So skip that third call. You should also check your variable names. `vatPercent` and `tvaPercent`? And what is `preTaxPrice`? – trincot Jul 03 '22 at 12:50
  • yes I'm working with Stimulus js, ttcPriceTargets is a div in twig Symfony – learn Jul 03 '22 at 12:50
  • Please, provide more details. – Mina Jul 03 '22 at 12:51
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 03 '22 at 23:17

1 Answers1

-1

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;
    }
}
sashok1337
  • 1,019
  • 1
  • 7
  • 14