codeMan
codeMan

Reputation: 5758

More elegant ways of converting YES/NO to true/false

There might be a very basic thing that might be missing. Please let me know if there is a better way to do this which is much more elegant than using the ternary operator in javascript.

if (data.isYellowCupcake === YES) {
  dataCopy.isCupcake = true;
} else if (data.isYellowCupcake === NO) {
  dataCopy.isCupcake = false;
}

Upvotes: 0

Views: 70

Answers (2)

Cray
Cray

Reputation: 2850

Even though you said you don't want ternary operator I will leave this here.

   dataCopy.isCupcake = data.isYellowCupcake === 'YES' ? true 
    : data.isYellowCupcake === 'NO' ? false 
    : null;

Upvotes: 2

chris
chris

Reputation: 2627

How about:

dataCopy.isCupkake = data.isYellowCupkake === YES;

By the way, the word cupcake only has one k.

Upvotes: 3

Related Questions