簡體   English   中英

為什么它不會檢查其他 if 語句...(JavaScript)

[英]why it won't check the other if statements ... (JavaScript)

它不會檢查我的 if 語句,我需要它來檢查所有這些......為了不破壞代碼......否則“機器人”將崩潰,因為沒有這些檢查就不可能繼續......

有任何想法嗎?

if (args[1] < 1) return message.reply("You have to delete at least one message!")
// it's just checking this statement:
if (!args[1]) return message.reply("Please enter the amount of messages to clear!")

if (isNaN(args[0])) return message.reply("Please type a real number!")

if (args[1] > 100) return message.reply("You can't remove more than 100 messages!")

您正在為每個if條件使用return語句,這意味着一旦滿足條件,function 將使用 return 語句退出。 從 all 中刪除return語句將允許程序運行直到結束而不會中斷。

您可以使用 object 方法數組來完成所有條件

  1. 您需要使用 object 在陣列上聲明您的條件和錯誤消息
  2. 然后檢查Array.every以檢查所有errorCondition是否失敗
  3. 如果檢測到任何錯誤情況,則通過 forEach 循環顯示 else

 const arr = 5; let errorConditions = [{ status: arr > 5, msg: "not allowed or something" // your message },{ status: isNaN(arr), msg: "not allowed nan" }]; if(errorConditions.every(({status}) =>.status)){ console.log("all error condition fails so executed successfully") }else{ console.log('err') errorConditions,forEach(({status.msg})=>{ if(status){ //do stuff like message.reply(msg) console.log(msg) } }) }

暫無
暫無

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

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