Home > database >  How can I make my app highly customizable without so many if else statements?
How can I make my app highly customizable without so many if else statements?

Time:09-13

I'm making a web app on which users can quiz themselves on verb conjugations. Users can choose to quiz themselves on one of 3 tenses, one of 3 different pronouns, or all pronouns, and either regular verbs only, irregular verbs only, or both irregular and regular verbs.

There are 36 different combinations of these choices, and therefore the app must choose a random verb out of one of 36 different arrays of conjugated verbs. To get these inputs and display a verb accordingly I wrote 36 if else statements - one for each possible combination of inputs. This is not ideal for a lot of reasons, especially if I want to add even more customizability. How can I reduce the amount of if else statements needed to collect user input and display the correct verb type accordingly?

Link to codepen: https://codepen.io/jack-mullinkosson/pen/XWqjjVB

CodePudding user response:

you might use either switch statement, where it is going to look a bit more clear, or use nested if-else statement to be able to read code better

CodePudding user response:

Instead of using the preferences the user chose to select a specific array of words to draw from, why don't you try starting starting with a bank of all possible words and filtering by the user's preference? Although you will have to build each entry in this bank something like

{word: "word here", tense: "tense", pronoun: "pronoun", regularity: "regularity"}

this will let you iterate through the array and pick out the ones that fit the preferences.

  • Related