Skip to content
Advertisement

Is there anyway to use less if statements in my function?

I have made a random password generator using a class called password and a method called generate.

My program works as it should. It generates a random password determined by the users preferences for length, upper or lowercase, numbers and special characters.

I was just wondering if there was a way to refactor the numerous if statements I have used to determine what sort of password the program would generate.

Any other suggestions for improvements I could make would also be helpful. Thanks a ton :D

Code:

JavaScript

Advertisement

Answer

Evaluate it in the initializer. It’s not less if checks per se, but it’s a bit cleaner.

JavaScript

If you actually want less checks, then just pass in the characters directly.

JavaScript
Advertisement