HTML/JavaScript: Why do I keep getting NaN? [closed]

  html, javascript

I’m trying to multiply the number of months (between two dates), with a number user input, and a constant number of 40.

The expected result is the product of the three numbers above however the actual result is NaN.

I checked the web browser console and found all my variables were number types but there were no error messages.

I’ve tried finding the type of every variable and breaking down the code to do it one by one. I’ve also researched multiplying in JavaScript and done as shown but nothing works.

here is my javascript calculation:

function calculatePrice() {
  let months = Math.round(ms / 1000 / 60 / 60 / 24 / 30)
  let p1 = document.getElementById("pallet-input").value;
  let p1n = parseFloat(p1);
  let price2 = p1n * 40.0 * months;
  if (!isNaN(price2)) {
    document.getElementById("grandTotal").innerHTML="$"+price2;
  }
}
<button onclick="calculatePrice()">Calculate</button>
<h2><span id="grandTotal">$</span></h2>

Source: Ask Javascript Questions

LEAVE A COMMENT