亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的 if-else 語句不能使用 OR ( | | ) 來工作?

為什么我的 if-else 語句不能使用 OR ( | | ) 來工作?

長風秋雁 2023-11-02 16:55:17
當我運行該程序時,控制臺給出一個錯誤,指出“星期日”(和“星期六”,基于用戶輸入)不存在。我在 if 語句中嘗試了多種帶括號的組合,但沒有任何效果。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 (getText(day) == "Sunday" || getText(day) == "Saturday") {  console.log("It's the weekend!");} else {  console.log("Can't wait for the weekend to get here.");}
查看完整描述

2 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

您應該刪除它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.");

}


查看完整回答
反對 回復 2023-11-02
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

如果用戶單擊“確定”或單擊“取消”,該prompt()函數始終返回 a 。在您的情況下,只要您輸入一些內容并單擊“確定”,就會出現一個字符串,可能是或。stringnullday"Sunday""Saturday"


但是,如果您希望將大寫字母和小寫字母視為相同,例如"sunday"相當于"Sunday",則應使用toLowerCase()ortoUpperCase()方法來確保兩個字符串要么全部小寫,要么全部大寫。像下面這樣:


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.");

}


查看完整回答
反對 回復 2023-11-02
  • 2 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號