0

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?

Assorted
  • 41
  • 1
  • 6

2 Answers2

3

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.

Dai
  • 141,631
  • 28
  • 261
  • 374
2
if (["good", "beautiful", "pretty", "excellent", "superb", "spectacular"].includes(a)) {...}
kindall
  • 178,883
  • 35
  • 278
  • 309