mercredi 9 septembre 2020

Is there a shorter way to this than mulitiple if statements?

I've been working on a project where it shows me games from the IGDB API using filters. But now I have a problem with multiple if statements.
It works but just looks ugly and isn't great code.

if (rating != "" && date != "" && platform != "") {
    return "where " + rating + " & " + date + " & " + platform + ";";
} else if (rating != "" && date == "" && platform == "") {
     return "where " + rating + ";";
} else if (rating != "" && date != "" && platform == "") {
     return "where " + rating + " & " + date + ";";
} else if (date != "" && rating == "" && platform == "") {
     return "where " + date + ";";
}

I've tried some different approaches but they didn't work.
How can I make this better?

Aucun commentaire:

Enregistrer un commentaire