Isoraqathedh

quiz-question.js (1st draft)

Dec 16th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Common function. Should be separated out to a separate file for reuse
  2. // (quiz-question-engine.js)
  3. function findChecked(question) {
  4.     // Finds which choices have been selected and which ones have not.
  5.     // Always returns an array of values that have been selected,
  6.     // even for the case of radio buttons (which can only have one selection).
  7.     // Is undefined in the case of an error.
  8.     switch (question[0].type) {
  9.         // We select what we should do based on the first element;
  10.         // this assumes that each question will only have one type of input.
  11.     case "radio":
  12.         for (var i = 0; i < question.length; i++) {
  13.             if (question[i].checked) {
  14.                 return [question[i].value];}}
  15.     case "checkbox":
  16.         var checkedBoxes = [];
  17.         for (var i = 0; i < question.length; i++) {
  18.             if (question[i].checked) {
  19.                 checkedBoxes.push(question[i].value);}}
  20.         return checkedBoxes;
  21.     default:
  22.         console.error("Incompatible input type found (want radio or checkbox)");
  23.         return undefined;}}
  24.  
  25. // Repeat EITHER compact OR expanded for each question in the function process.
  26. // The difference is that of mere formatting.
  27. // The difference between radio and checkboxes is dealt with in `findChecked',
  28. // so they all look the same.
  29.  
  30. // Compact:
  31. for (choice in findChecked(f.one)) {
  32.     switch (choice) {
  33.     case "1": glyph++; break;
  34.     case "2": item++; break;
  35.     case "3": incantation++; break;
  36.     case "4": potion++; break;
  37.     case "5": glyph++; break;
  38.     case "6": aether++; break;}}
  39.  
  40. // Expanded:
  41. for (choice in findChecked(f.one)) {
  42.     switch (choice) {
  43.     case "1":
  44.         glyph++;
  45.         break;
  46.     case "2":
  47.         item++;
  48.         break;
  49.     case "3":
  50.         incantation++;
  51.         break;
  52.     case "4":
  53.         potion++;
  54.         break;
  55.     case "5":
  56.         glyph++;
  57.         break;
  58.     case "6":
  59.         aether++;
  60.         break;}}
Advertisement
Add Comment
Please, Sign In to add comment