簡體   English   中英

我缺少什么,以便我的Logic運算符可以對if-else語句正常工作?

[英]What I am I missing so that my Logic operator works properly for my if-else statement?

我的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> 

當提示您返回“今天是您的生日。生日快樂!”的提示時,我應該能夠輸入當前的月份和日期。

不要使用嚴格等於( === )。 標准==應該在這里工作,因為您的數據類型不兼容: 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); 

建立在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!";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM