Number函数返回字符串

时间:2016-07-16 14:04:06

标签: javascript function

我正在制作一个程序,它将使用用户的出生日期来计算当前年龄,但即使在使用Number()之后,变量的typeof()也没有改变并保持string.i我只是没有得到错误的地方是

<!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>

1 个答案:

答案 0 :(得分:2)

您没有将值分配回原始变量

setArguments()

基本上month = Number(month); 函数会返回Number类型的值。你必须在一个变量中接收它才能使用它。