[英]Age calculator: checking for a blank input or a string input
I have a function that asks you questions to determine your age. 我有一个功能,问您一些问题来确定您的年龄。 I want to cancel the function and issue an alert if the user enters a string value or doesn't enter anything at all. 我想取消该功能并在用户输入字符串值或根本不输入任何内容时发出警报。
Here's my code so far: 到目前为止,这是我的代码:
<button id="btn"> Calculate Age </button>
<p id="para"></p>
document.getElementById("btn").addEventListener("click", ageCalc);
function ageCalc () {
// Calculate year of birth
var year = prompt("What year were you born?");
if (parseInt(year) < 1917) {
return alert("Please enter a valid year");
}
// Calculate month of birth
var month = prompt("Numerically, what month were you born?");
if (parseInt(month) > 12 || parseInt(month) < 1 ) {
return alert("Please enter a valid month!");
}
// Calculate day of birth
var day = prompt("What day were you born?");
if(parseInt(day) > 32 || parseInt(day) < 1) {
return alert("Please enter a valid day!");
}
// Calculate age based on user input
var presentDate = new Date ();
var birthDate = new Date(year,month,day);
var age = (presentDate - birthDate) / 1000 / 60 / 60 / 24 / 365;
return document.getElementById("para").innerHTML = "You are " + Math.floor(age) + " years old.";
}
What is the proper move here? 这是什么适当的举动? I assume it will be tweaking the if statement somehow to check for strings or blank statements but i'm kinda lost. 我认为它将以某种方式调整if语句以检查字符串或空白语句,但是我有点迷失了。 Any thoughts guys? 有什么想法吗?
You can exclude null/undefined/empty year
values by simple adding an if check. 您可以通过简单地添加if检查来排除空/未定义/空year
值。 Also, always add the second radix
parameter for parseInt method: 另外,请始终为parseInt方法添加第二个radix
参数:
var year = prompt("What year were you born?");
if (!year || parseInt(year, 10) < 1917) {
return alert("Please enter a valid year");
}
You can do the same for month
and day
. 您可以在month
和day
执行相同的操作。
document.getElementById("btn").addEventListener("click", ageCalc);
function ageCalc () {
// Calculate year of birth
var year = prompt("What year were you born?");
if (parseInt(year) < 1917) {
return alert("Please enter a valid year");
}
else if (year == null)
return;
// Calculate month of birth
var month = prompt("Numerically, what month were you born?");
if (parseInt(month) > 12 || parseInt(month) < 1 ) {
return alert("Please enter a valid month!");
}
else if (month == null)
return;
// Calculate day of birth
var day = prompt("What day were you born?");
if(parseInt(day) > 32 || parseInt(day) < 1) {
return alert("Please enter a valid day!");
}
else if (day == null)
return;
// Calculate age based on user input
var presentDate = new Date ();
var birthDate = new Date(year,month,day);
var age = (presentDate - birthDate) / 1000 / 60 / 60 / 24 / 365;
return document.getElementById("para").innerHTML = "You are " + Math.floor(age) + " years old.";
}
Just check if the input is correctly parsed when using parseInt()
by checking if the result of it isn't NaN
like this: 只需通过检查输入结果是否不是NaN
来检查使用parseInt()
时输入是否正确解析:
if( parseInt(month) == NaN ){ alert("invalid input"); return false; }
in case a user type a non int parseable input it will stop the execution. 如果用户键入一个非int可解析的输入,它将停止执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.