Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript calculations return NaN as result

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.

like image 490
Vishwasa Navada K Avatar asked Jan 28 '26 07:01

Vishwasa Navada K


1 Answers

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;
like image 189
VisioN Avatar answered Jan 29 '26 19:01

VisioN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!