0

I am developing a simple application form where I am calculating experiences from maximum three employers. Now I want to add them up . The experiences are in the form of X years Y months and Z days. I have written following javascript function --

function total(){
    var td;
    var fd=parseInt(document.getElementById("LoS_days1").value);
    var sd=parseInt(document.getElementById("LoS_days2").value);
    var ld=parseInt(document.getElementById("LoS_days3").value);
    var tm;
    var fm=parseInt(document.getElementById("LoS_months1").value);
    var sm=parseInt(document.getElementById("LoS_months2").value);
    var lm=parseInt(document.getElementById("LoS_months3").value);
    var ty;
    var fy=parseInt(document.getElementById("LoS_year1").value);
    var sy=parseInt(document.getElementById("LoS_year2").value);
    var ly=parseInt(document.getElementById("LoS_year3").value);
    td = (fd +sd +ld);
    var rd = td%30;
    var cm = Math.floor(td/30);
    document.getElementById("Totalexp_day").value=rd;   
    tm = (cm + fm +sm +lm);
    var rm = tm%12;
    var cy = Math.floor(ty/12);
    document.getElementById("Totalexp_month").value=rm;
    ty = (cy + fy +sy +ly);
    document.getElementById("Totalexp_year").value=ty;
    }

I am getting a NaN message in each of the Totalexp_day, Totalexp_month and Totalexp_day field. Earlier I had some modified code that was not showing NaN message but it was not showing the desired results. Kindly suggest what to do to eliminate these two errors.

JJJ
  • 32,902
  • 20
  • 89
  • 102
sdk
  • 1
  • 3
  • var cy = Math.floor(ty/12); will be var cy = Math.floor(tm/12); then also it is not working – sdk Mar 01 '15 at 12:05
  • Are the elements from which you retrieve the data inputs, or other HTML-elements? Ergo, could you please post the corresponding HTML. – Mouser Mar 01 '15 at 12:07
  • This has eliminated NaN message but I ma getting an errorneous result. Please help. – sdk Mar 01 '15 at 12:09
  • With the `ty` to `tm` change the code works just fine: http://jsfiddle.net/wzavtspu/ What result are you expecting, and what do you get? – Guffa Mar 01 '15 at 12:10
  • I want to add up three date values, namely days months and years – sdk Mar 01 '15 at 12:11
  • I am providing input values which are text fields in html form . The three input values for three days is 27 27 and 27. The three values for month values are 5 5 and 5. Finally three values for year are 7 7 and 7. As a result I ma getting days total as 27, months total as 3 and year total as 757546777, which is incorrect. Kindly help. – sdk Mar 01 '15 at 12:19

2 Answers2

0
parseInt(document.getElementById("LoS_days1").value)

if the first character of the string cannot be converted to a number, parseInt will return NaN.

To avoid this, you can so something like is suggested here:

parseInt(document.getElementById("LoS_days1").value) || 0
Community
  • 1
  • 1
wimh
  • 15,072
  • 6
  • 47
  • 98
0

If document.getElementById("Totalexp_day").value is empty then also it will return NaN. Make sure you have some number there.

Second alternative is reading document.getElementById("Totalexp_day").innerHTML and then applying parseInt

Probably you alert or console log the document.getElementById("Totalexp_day").value you would be more clearer why this problem is comming

Ajit kohir
  • 481
  • 5
  • 14