How to optimize the test conditions in the form?

Hello. Please help me to optimize the code of check of conditions on the checkout page.

There is a check made on the constructor that allows you to insert custom html-css-js code.

There is a form in PHP that accepts requests that are sent after the first login to the order page and returns the user information from the database.

On the basis of this information are made check.

For example, if the returned hasDiagnostic === "true" and workedWith === "some string", then do the following:
if (hasDiagnostic = = = true) {

 if (workedWith !== undefined) {
 $(`option[value="${workedWith}"]`).attr("selected", "selected");

 switch (workedWith) {
 case "Lily":
 typeConsultation.prop("checked", true);;
 liliyaConsultation.prop("checked", true);;
 case "Alexander":
 typeConsultation.prop("checked", true);;
 alexConsultation.prop("checked", true);;

This is only a small piece of code, I have it on for 15 such "cases".

It can somehow be optimized or have to put up with a huge number of cases and conditions?

Or another case, more difficult for me.

Need to monitor changes in two parameters, for example: select the name of the specialist and the type of service. Before that, of course, the script is requested from the database user information and returned them.

And then begins the dance:
- (if the selected service type # 1 and employee) And (value from database === the name of the employee), then everything is OK, do nothing
- (if the selected service type # 1 and employee) And (the value from database !== the name of the employee), we deduce a warning
- (if the selected service type # 2 and the employee) And (value from database === the name of the employee and another value from database === true), then everything is OK, do nothing

And so on. Here a stupor as it is all taken into account when checking if the user can freely switch service types (there is a radio button) and choose employee from the drop down list?

To make a big list of conditions that will be checked when changing the type of service? Type input[id="type"].change (), and drove inside to check all the conditions, if the change of radio button.

And separately select[option="Name Surname"].change () is for EACH employee in the list - and also a similar test, only matched by type of service?

Like a simple test is, in fact, but I have 200 lines of code out. Know what you're doing wild vermishel of code, but how to optimize - I do not understand :(

Prompt, please, any ideas on optimization of such inspections.
April 4th 20 at 13:28
1 answer
April 4th 20 at 13:30
1. liliyaConsultation, alexConsultation and other what is the difference? If all the same acre of the selected employee, then write a function that will take the selected employee, in order for each one, and also not to write.

2. Sign on a piece of paper first, all the model actions, then find the monotony of data/running conditions.

3. Take the time to write all the code at once, do it little by little and check, for example "Choose an employee", he clicked all, yeah, everything is OK, move on.

4. typeConsultation.prop("checked", true); and; more than confident that you can make for the conditions (outside the brackets, remember the math).

5. Good luck))
1. liliyaConsultation and alexConsultation are variables that I have recorded values of input purely for ease of use. Differ only in the value:
var liliyaConsultation = $('input[value="424000"]');
var alexConsultation = $('input[value="424001"]');

2. Thanks for the advice, will make a flowchart to make it clearer)

3. So for the parts I wrote all of the preceding conditions, all turned in the sheet code. Now, when it is necessary to check the conditions when changing the individual or changing the type of services, it comes to mind only .change() and inside for everyone to write hundreds of conditions...

4. Write a function of type chooseConsultation(), inside of which will be selected the check-box and click done and call the function wherever you need these 2 conditions to do this?

About functions in General also began to wonder when the code grew. Do you think it will be a normal practice if I have a function (can work with errors, I write in haste):
function checkDiagnosticAndWorker(typeDiagnostic, workedWith) {
 if (":checked") &&
 ) {
 "The selected diagnosis and the employee with whom he worked, everything is OK."
 } else if (workedWith !== undefined) {
 if (":checked")) {
 console.log("Selected diagnosis and people with no one worked, everything is OK.");

And I call it each time you change the type of service And at each change in drop-down list:
checkDiagnosticAndWorker(typeDiagnostic, workedWith);

Provided, of course, that workedWith defined when the page loads.

5. Thank you! :)) - Garrison_Bi commented on April 4th 20 at 13:33
checkDiagnosticAndWorker - at the development stage will be useful as logging in/debugging - ashley_Thiel21 commented on April 4th 20 at 13:36
@ashley_Thiel21, can be a little more, do not understand how useful and would be useful in the future?) - Garrison_Bi commented on April 4th 20 at 13:39
@Garrison_Bithat will stick in your input or whatever, the console will be able to see the elapsed event or not, sometimes these things help. Well, based on it to do all the logic. - ashley_Thiel21 commented on April 4th 20 at 13:42
@ashley_Thiel21, but is based on it to do the logic I wrote above? Separately to make code execution functions and call the function when triggered some sort of trigger to check, right? - Garrison_Bi commented on April 4th 20 at 13:45
@Garrison_Bi, Yes, as an option - ashley_Thiel21 commented on April 4th 20 at 13:48

Find more questions by tags JavaScriptjQuery