0

Here's the updated script I wrote

<script>
  let num1=parseFloat(document.querySelector('#origin').value)
  let num2=parseFloat(document.querySelector('#added').value)
  let calcProfit=parseFloat(100-num2)
  let btn=document.querySelector('#submit')
  btn.addEventListener("click", function(event){
      event.preventDefault()
      const initResult=num1/calcProfit
      const finalResult=initResult*100
      document.querySelector('span').innerHTML=`${finalResult.toFixed(3)}`
      console.log(typeof(finalResult))
      if (finalResult.toString().length>3 && finalResult.toString().length<7){
          const k=finalResult/1000
          document.querySelector('span').innerHTML=`${k.toFixed(1)}k`
        }
        if (finalResult.toString().length>6 && finalResult.toString().length<10){
            const m=finalResult/1000000
            document.querySelector('span').innerHTML=`${m.toFixed(1)}M`
        }
    });
   let clr=document.querySelector('#clear');
    clr.addEventListener("click", function(e){
        window.location.reload()
    })

at first run it shows NaN, but when I reload the page everything works just fine. I'm suspecting the clear function because when I first wrote it, it didn't give any errors but the whole app behaved abruptly. In this version the clear button removes the result but doesn't affect it, so when I put new numbers it gives me the old result. I tried innerHTML but that doesn't cut it.

Ray M
  • 72
  • 6
  • 1
    it would be very helpful if you build a snippet to show the behavior – Aalexander Jan 22 '21 at 13:10
  • You've multiple superfluous `parseFloat`s, `parseFloat(document.querySelector('span').innerHTML=\`${k.toFixed(1)}k\`)` adds a value to the DOM, but the return value of `parseFloat` is never used. – Teemu Jan 22 '21 at 13:13

1 Answers1

0

I got the answer from here javascript error [object HTMLInputElement][object HTMLInputElement]

there are 2 contributing factors to this error, I kept using multiple variables to make one calculation and using the parse methods. It doesn't show NaN anymore but the reload issue still persists.

Ray M
  • 72
  • 6