2 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
您應(yīng)該刪除它getText,它將正常工作:
var day = prompt('Enter a day of the week.');
console.log('Day is: ' + day);
if (day == 'Sunday' || day == 'Saturday') {
console.log("It's the weekend!");
} else {
console.log("Can't wait for the weekend to get here.");
}

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果用戶單擊“確定”或單擊“取消”,該prompt()函數(shù)始終返回 a 。在您的情況下,只要您輸入一些內(nèi)容并單擊“確定”,就會(huì)出現(xiàn)一個(gè)字符串,可能是或。stringnullday"Sunday""Saturday"
但是,如果您希望將大寫(xiě)字母和小寫(xiě)字母視為相同,例如"sunday"相當(dāng)于"Sunday",則應(yīng)使用toLowerCase()ortoUpperCase()方法來(lái)確保兩個(gè)字符串要么全部小寫(xiě),要么全部大寫(xiě)。像下面這樣:
var day = prompt("Enter a day of the week.");
console.log("Day is: " + day);
//if user input is equal to Sunday OR user input is equal to Saturday,
if (day.toLowerCase() == "sunday" || day.toLowerCase() == "saturday") {
console.log("It's the weekend!");
} else {
console.log("Can't wait for the weekend to get here.");
}
添加回答
舉報(bào)