0

i am making a program that will calculate the present age using user's date of birth but even after using the Number() ,the typeof() of variables aren't changing and remain string.i am just not getting where the mistake is?

<!DOCTYPE html>
<html>
<body>

<script>

function myfunction()
{
var name=prompt("enter your name");
 document.getElementById('name').innerHTML=name ;
  var year=prompt("enter your birth year");
  year=Number(year);

   document.getElementById('year').innerHTML=year;

  var month=prompt("enter your birth month(month number)");
  if(month>12)
  {
  alert("not possible,try again");
 month=prompt("enter your birth month(month number)");
month=Number(month);
  document.getElementById('month').innerHTML=month;
  }
  else
  {
  document.getElementById('month').innerHTML=month;
  var day=prompt("enter your birth day (DAY NO.)");
   }
  if(month==2&&day>28)
  {
  alert("you're drunk,come later");
  day=prompt("enter your birth day no.(ONLY THE DAY)");
  day=Number(day)
  document.getElementById('day').innerHTML=day;
 }
   if(day>31)
 {
alert("wrong day");
day=prompt("enter your birth day no.(ONLY THE DAY)");
day=Number(day);
document.getElementById('day').innerHTML=day;
 }
else
  document.getElementById('day').innerHTML=day;

 myage(year,month,day);
}
function myage(year,month,day)
{
  var date= new Date;
  var y,m,d,Y,M,D;
  y=date.getYear;
  m=date.getMonth;
  d=date.getDay;
  Y=y-year;
  M=m-month;
  D=d-day;
  document.getElementById('age').innerHTML=Y+"<br>"+M+"<br>"+D;

}



</script>

 <p id="name"><h1>NAME:</h1></p>
   <p id="year"><h1>YEAR</h1></p>
   <p id="month"><h1>MONTH:</h1></p>
   <p id="day"><h1>DAY:</h1></p>
    <p id="age"><h2>your age is=</h2></p>
   <button onclick="myfunction()">click to login</button>

  </body>
</html>
  • You're ignoring the return value of `Number`. `year = Number(year)` –  Jul 16 '16 at 14:05
  • Here's the [Google search](https://www.google.com/search?q=site%3Astackoverflow.com+javascript+Number+function+doesn%27t+work&gws_rd=ssl) I used to find the duplicate question/answer, for future reference. –  Jul 16 '16 at 14:08
  • i did what you asked me to , but still returning NaN. – Kaushani RC Jul 16 '16 at 14:13
  • @squint you marked the duplicate, i cannot find the original question. please mention the link ,sir. – Kaushani RC Jul 16 '16 at 15:24
  • The link is now at the top of your question. –  Jul 16 '16 at 15:43
  • ... look at this code:`y=date.getYear; m=date.getMonth; d=date.getDay;` You're *assigning* those functions to the variables instead of invoking them. Should be `y=date.getYear(); m=date.getMonth(); d=date.getDay();` –  Jul 16 '16 at 15:45

1 Answers1

2

You are not assigning the value back to the original variable,

month = Number(month);

Basically Number function would return a value of type number. You have to receive it in a variable to make use of that.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130