Home > Net >  JS mathematic, live counting
JS mathematic, live counting

Time:08-04

I am trying to make live mathematic calculation based on characters count. Counting of characters works, but other variables like dividing this sum by 1500 or multiplication of this sum by 30, doesn't work. Console tell's me nothing, but in text I am getting "NaN".

I am beginner in mathematics in JS and i don't see why it does not works. All sources in google tells me that it is good writed.

I am attaching screen of html and code under this text. On screen is pasted exactly 3000 letters.

Please help <3

enter image description here

<script type="text/javascript">
function count()
{
  var total=document.getElementById("text").value;
  total=total.replace(/\s/g, '');
  document.getElementById("total").innerHTML="total: <span>" total.length "</span>";

    var divided=total.lenght/1500;
    document.getElementById("divided").innerHTML="divided: <span>" divided "</span>";

    var price=divided*30;
    document.getElementById("pricing").innerHTML="price: <span>" price "</span>";
}
</script>


    

    <div id="pricing-counter">
<p id="total">total:<span>0</span></p>
<p id="divided">divided:<span>0</span></p>
<p id="pricing">price:<span>0</span></p>
    </div>

CodePudding user response:

You had a typo in the line where you set divided

function count()
{
  var total=document.getElementById("text").value;
  total=total.replace(/\s/g, '');
  document.getElementById("total").innerHTML="total: <span>" total.length "</span>";

    var divided=total.length/1500; // Typo corrected
    document.getElementById("divided").innerHTML="divided: <span>" divided "</span>";

    var price=divided*30;
    document.getElementById("pricing").innerHTML="price: <span>" price "</span>";
}
 <div id="pricing-counter">
<input id="text"></input>
<button onclick="count()">Count</button>
<p id="total">total:<span>0</span></p>
<p id="divided">divided:<span>0</span></p>
<p id="pricing">price:<span>0</span></p>
    </div>

  • Related