vendredi 29 juillet 2016

Results based on multiple variables (flowchart style)

I'm trying to find a way to convert a bunch of else if's to a simple function to find out a result for two possibilities. It compares answers from two questions to produce an answer.

The defined variable 'usage' is

var usage = {
  '1': { value: false, num: 1, string: 'gaming' },
  '2': { value: false, num: 2, string: 'browsing the web' },
  '3': { value: false, num: 3, string: 'streaming video' },
  '4': { value: false, num: 4, string: 'streaming music' }
};

The code for determining the outcome goes as follows.

'firstSelected' detects whether the first option of the second question is selected.

// Check if more than one option is selected on first question
if (selectedOptions.length > 1) {
  maxPackage();
}

// Check if gaming is selected on first question
else if (usage['gaming'].value == true) {
  maxPackage();
}

// Check if web is selected on the first question and firstSelected is true
else if ((usage['web'].value == true) && (firstSelected == true)) {
  standardPackage();
}

// Check if web is selected on the first question but firstSelected is false
else if ((usage['web'].value == true) && (firstSelected == false)) {
  maxPackage();
}

// Check if video is selected on the first question
else if (usage['video'].value == true) {
  maxPackage();
}

// Check if music is selected on the first question and firstSelected is true
else if ((usage['music'].value == true) && (firstSelected == true)) {
  standardPackage();
}

// If none of these apply show Max Package
else {
  maxPackage();
}

Thanks!

Aucun commentaire:

Enregistrer un commentaire