lundi 27 septembre 2021

How to check and list all false conditionals in if statement - JavaScript?

I'm trying to improve my code and have better logging.

Is there an ideal way of how to tell which conditions are false in a list of conditionals?

i.e)

if(isAnimal && isCarnivore && isPlant){
 // want to console.log all the false conditions
}

We could write

let falseString = ""


if (!isAnimal) {
 falseString = falseString + "is not an Animal";
} 

if (!isCarnivore) {
 falseString = falseString + "is not a Carnivore";
}

if (!isPlant) {
 falseString = falseString + "is not a Plant";
}

console.log("string of false conditions" , falseString)

Then this would log a string of which conditions are false, but this seems to be a naive solution.

What is a better way to do this in JavaScript?

Thank you!

Aucun commentaire:

Enregistrer un commentaire