Vlad Danila
Vlad Danila

Reputation: 1372

Generate random boolean attribute TypeScript

How can one assign to a an object which has a boolean property random values of true or false?

items: any[];

items.forEach( (item) => {
   item.randomBooleanProperty = ...; //true of false
});

Upvotes: 1

Views: 1561

Answers (1)

Ron F
Ron F

Reputation: 390

You can try this example: In this code, we check whether the random number which is between 0 and 1 is greater than 0.5, so we have a 50% to get true and 50% to get false.

items: any[];

items.forEach( (item) => {
   item.randomBooleanProperty = Math.random() >= 0.5; //true of false
});

Upvotes: 5

Related Questions