0
我正在製作一個程序,它將使用用戶的出生日期計算當前年齡,但即使在使用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>
你忽略了'Number'的返回值。 'year = Number(year)' –
這裏是[Google搜索](https://www.google.com/search?q=site%3Astackoverflow.com+javascript+Number+function+doesn%27t+work&gws_rd=ssl)我曾經找到重複的問題/答案,以供將來參考。 –
我做了你所要求的,但仍然返回NaN。 –