简体   繁体   中英

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..

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. Removing the return statement from all will allow the program run til the end without interruption.

You could do all you condition with array of object method

  1. you need declare your condition and error message on array with object
  2. Then check with Array.every to check all the errorCondition are fails
  3. If you have any error condition detected then display with else via forEach loop

 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) } }) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM