Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting NaN when adding variables which consists of another variables

Tags:

javascript

nan

var dml = 30
var dd = parseFloat(document.getElementById("DriverD").value)     <----- Only numbers like 10
var dm = dd-dml

alert((dd - dml) * 0.75)    <----- This works
alert(dm * 0.75)            <----- This returns NaN
alert(typeof(dm))        <----- This show that dm is a Number

I'm not sure why I keep getting NaN. I already tried parseFloat and parseInt but still showing NaN when multiplying a variable (dm) which consists of variables (dd-dml). dm is the result of subtracting dml with dd or 10-30. Please share your solutions.

I'm new here and I need help, please don't troll :) I am trying to add a cost calculator to my website.

like image 766
Jay Avatar asked Nov 12 '22 14:11

Jay


1 Answers

It seems fine to me.

I want to tell another possible problem: You dont check if document.getElementById("DriverD").value is a number or not. If a user enters a string or other type, it will cause a problem.

like image 112
smttsp Avatar answered Nov 15 '22 04:11

smttsp