I have a code in my project but it is kind of clunky. Could anyone help me to simplify the following code?
if (a == "good" || a == "beautiful || a == "pretty"|| a == "excellent" || a == "superb"|| a == "spectacular")
Could I make this into some sort of array and then use that array in this if code?
I suppose you could use a Hash-Set structure instead: in JS this is Set
(or Set<T>
in TypeScript):
const positiveWords = new Set( [ "good", "beautiful", "pretty", "excellent", "superb", "spectacular" ] );
Usage:
if( positiveWords.has( a ) ) {
}
Note that you'll need to convert a
to lowercase first. If you want case-insensitive (or accent-insensitive, or other culture/locale-specific comparison rules) then use Intl
and/or localeCompare
.