JavaScript calculations return NaN as result
forms, javascript, nan
Solution
In some cases `+` signs in your formula will do string concatenation instead of sum, as in JavaScript `"1" + 1 === "11"`. You need to convert your values from strings (as returned from form fields) to numbers with `parseInt` or `parseFloat` functions:
var d = parseInt(document.getElementById("dd").value, 10);
or to support float numbers (if required):
var d = parseFloat(document.getElementById("dd").value);
or a shortcut of `Number(v)`:
var d = +document.getElementById("dd").value;
Problem
I am developing a html page that takes date and displays day. I am using a formula called Zeller's congruence. But in JavaScript the formula returns the Result "NaN". I googled the problem. Couldn't figure out the solution. Here is the html that takes values. ``` <form method="post"> <br/> day:<input id="dd" name="dd" type="text"/><br/> month:<input id="mm" name="mm" type="text"/><br/> year:<input id="yy" name="yy" type="text"/><br/> <input type="submit" value="go" onclick="day()"/><br/> </form> ``` Here is the piece of JavaScript formula thats returning NaN. ``` function day() { var d=document.getElementById("dd").value; var m=document.getElementById("mm").value; var y=document.getElementById("yy").value; var h=(d+(((m+1)*26)/10)+y+(y/4)+6*(y/100)+(y/400))%7;//returns NaN var d2=((h+5)%7); code continues.. ``` Please help me. Thanks in advance.