简体   繁体   English

为什么它不会检查其他 if 语句...(JavaScript)

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

It won't check my if statements, I need it to check all of them.. in order to not break the code... otherwise the "Bot" will crash as it's not possible without those checks to continue..它不会检查我的 if 语句,我需要它来检查所有这些......为了不破坏代码......否则“机器人”将崩溃,因为没有这些检查就不可能继续......

Any ideas?有任何想法吗?

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!")

you are using the return statement for each if conditional which means as soon as the condition is met, the function will exit with the return statement.您正在为每个if条件使用return语句,这意味着一旦满足条件,function 将使用 return 语句退出。 Removing the return statement from all will allow the program run til the end without interruption.从 all 中删除return语句将允许程序运行直到结束而不会中断。

You could do all you condition with array of object method您可以使用 object 方法数组来完成所有条件

  1. you need declare your condition and error message on array with object您需要使用 object 在阵列上声明您的条件和错误消息
  2. Then check with Array.every to check all the errorCondition are fails然后检查Array.every以检查所有errorCondition是否失败
  3. If you have any error condition detected then display with else via forEach loop如果检测到任何错误情况,则通过 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