本文介绍了数字函数返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个程序,该程序将使用用户的出生日期来计算当前年龄,但是即使使用了Number()之后,变量的typeof()也不会改变并且仍然是字符串.我只是没有弄错哪里是吗?
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 are not assigning the value back to the original variable,
month = Number(month);
基本上 Number
函数将返回 number
类型的值.您必须将其接收到变量中才能使用.
Basically Number
function would return a value of type number
. You have to receive it in a variable to make use of that.
这篇关于数字函数返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!