问题描述
我的if-else代码块运行得很好,除了最后一个使用AND逻辑运算符的条件。 我是JavaScript的新手,我无法弄清楚哪里出了问题! 想法?
我尝试添加方括号,删除方括号并重新调整语句。
//declare variables the prompts the user to state their birthdate in //month, day and year var birthYear = prompt('What year were you born in?'); var birthMonth = prompt('What month were you born in? In numerals please!'); var birthDay = prompt('What day of the month were you born on? In numerals please!'); //declare variables to get current date var now = new Date(); var currentYear = now.getFullYear(); var currentMonth = now.getMonth() + 1; //so now January = 1 var currentDay = now.getDate(); //declare variable for age will turn this year var age = currentYear - birthYear; //declare variable for text output var text = ""; //create if/else loop for three different scenarios if (birthMonth < currentMonth) { text += "You have turned " + age + " years old already this year."; } else if (birthMonth > currentMonth) { text += "You will be turning " + age + " years old later this year."; } else if (birthMonth === currentMonth && birthDay === currentDay) { text += "Today is your Birthday! Happy Birthday!"; } document.getElementById('agestate').innerHTML = text;
<p id="agestate"></p>
当提示您返回“今天是您的生日。生日快乐!”的提示时,我应该能够输入当前的月份和日期。
1楼
不要使用严格等于( ===
)。
标准==
应该在这里工作,因为您的数据类型不兼容: birthMonth
是字符串,而currentMonth
是整数,因此严格相等将失败。
或者,您可以在比较之前将字符串转换为数字(反之亦然)。
您可以尝试以下最新更新的代码(在提示中输入今天的日期以显示“今天是您的生日!”)
//declare variables the prompts the user to state their birthdate in //month, day and year var birthYear = prompt('What year were you born in?'); var birthMonth = prompt('What month were you born in? In numerals please!'); var birthDay = prompt('What day of the month were you born on? In numerals please!'); //declare variables to get current date var now = new Date(); var currentYear = now.getFullYear(); var currentMonth = now.getMonth() + 1; //so now January = 1 var currentDay = now.getDate(); //declare variable for age will turn this year var age = currentYear - birthYear; //declare variable for text output var text = ""; //create if/else loop for three different scenarios if (birthMonth < currentMonth) { text += "You have turned " + age + " years old already this year."; } else if (birthMonth > currentMonth) { text += "You will be turning " + age + " years old later this year."; } else if (birthMonth == currentMonth && birthDay == currentDay) { text += "Today is your Birthday! Happy Birthday!"; } console.log(text);
2楼
建立在FrankerZ答案的基础上, prompt
函数返回的是字符串而不是数字,但是我不鼓励使用==
因为它是JavaScript的一种严重的随机行为,在任何其他试图比较字符串和数字的(理性)语言中,它都不会编译或引发错误,但是javascript具有隐藏的行为且具有松散的相等性,您可以了解更多信息
我鼓励您使用数字解析和===
代替(也可以处理遗漏的情况):
//declare variables the prompts the user to state their birthdate in
//month, day and year
var birthYear = Number(prompt('What year were you born in?'));
var birthMonth = Number(prompt('What month were you born in? In numerals please!'));
var birthDay = Number(prompt('What day of the month were you born on? In numerals please!'));
//declare variables to get current date
var now = new Date();
var currentYear = now.getFullYear();
var currentMonth = now.getMonth() + 1; //so now January = 1
var currentDay = now.getDate();
//declare variable for age will turn this year
var age = currentYear - birthYear;
//declare variable for text output
var text = "";
//create if/else loop for three different scenarios
if (birthMonth < currentMonth) {
text += "You have turned " + age + " years old already this year.";
} else if (birthMonth > currentMonth) {
text += "You will be turning " + age + " years old later this year.";
} else if (birthMonth === currentMonth && birthDay === currentDay) {
text += "Today is your Birthday! Happy Birthday!";
} else (birthMonth === currentMonth && birthDay !== currentDay) {
text += "Your birthday is this month! Lucky you!";
}