mercredi 3 avril 2019

How to simplify a nested if statement tree

I have a nested if statement tree that runs a function 16 times (I know), each time sending in a different set of elements to the function.

The function simply returns true or false.

if(!checkSpecial(1,1,1,1)) {
    if(!checkSpecial(1,1,1,0)) {
        if(!checkSpecial(1,1,0,1)) {
            if(!checkSpecial(1,0,1,1)) {
                if(!checkSpecial(0,1,1,1)) {                                
                    if(!checkSpecial(1,1,0,0)) {                            
                        if(!checkSpecial(1,0,0,1)) {                            
                            if(!checkSpecial(0,0,1,1)) {                            
                                if(!checkSpecial(1,0,1,0)) {                            
                                    if(!checkSpecial(0,1,0,1)) {                            
                                        if(!checkSpecial(0,1,1,0)) {                    
                                            if(!checkSpecial(1,0,0,0)) {                    
                                                if(!checkSpecial(0,1,0,0)) {                    
                                                    if(!checkSpecial(0,0,1,0)) {                    
                                                        if(!checkSpecial(0,0,0,1)) {                    
                                                            if(!checkSpecial(0,0,0,0)) {
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }   
} else {
    // do other stuff
}

as you can see, if the function returns false in every single one of those instances, I want to do other things.

I don't want to do anything if the function returns true.

My question is, I know there has to be a better way of doing this, I'm assuming through some sort of loop, but I'm not aware of what this type of loop would be called or how it would work.

My fix so far:

for (var i = 0; i < 16; i++) { 
   // HELP!
}

Any pointers would be appreciated. thank you.

Aucun commentaire:

Enregistrer un commentaire