user3064626 user3064626 - 2 months ago 5x
Javascript Question

Javascript: Compare three booleans and return all true booleans

I have a method getPreferredfood(). It gets three boolean values that represent different food items. Which ever of them are true, you return those.

getPreferredFood: function (){
var item 1;
var item2 ;
var item3;


return "Preferred food is " (whichever item was true)

I'm not sure how to return the the item that are true.


It looks like you want to use the variable name as part of a string output. That's not usually how you want to do things.

Consider doing this instead:

var tags = {
    "vegan": true,
    "non-veg": false,
    "veg": false

var true_tags = [];
for (var tag in tags) {
    if (tags[tag] === true) {

console.log(true_tags); // will print [ "vegan" ]