Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. <html>
  2. <head></head>
  3. <body>
  4. <form id="form"></form>
  5. <br/>Possible solutions:
  6. <ul id="ul"></ul>
  7. </body>
  8. <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  9. <script type="text/javascript">
  10. var questions = [
  11. { bools:[1,1,1,1,0,0,0,0], text:"Printer does not print" },
  12. { bools:[1,1,0,0,1,1,0,0], text:"A red light is flashing" },
  13. { bools:[1,0,1,0,1,0,1,0], text:"Printer is unrecognized" },
  14. ];
  15. var answers = [
  16. { bools:[0,0,1,0,0,0,0,0], text:"Check the power cable" },
  17. { bools:[1,0,1,0,0,0,0,0], text:"Check the printer-computer cable" },
  18. { bools:[1,0,1,0,1,0,1,0], text:"Ensure printer software is installed" },
  19. { bools:[1,1,0,0,1,1,0,0], text:"Check/replace ink" },
  20. { bools:[0,1,0,1,0,0,0,0], text:"Check for paper jam" },
  21. ]
  22.  
  23. $(document).ready(function() {
  24. // Init form with questions. "Value" is a descending power of 2.
  25. var value = questions[0].bools.length;
  26. for (var i = 0; i < questions.length; i++) {
  27. value /= 2;
  28. var el = '<br /><input type="checkbox" value="' + value + '">' + questions[i].text;
  29. $("#form").append(el);
  30. }
  31.  
  32. // Respond to a checkbox action.
  33. $('input:checkbox').change(function() {
  34.  
  35. // Figure out which combination of checkboxes the user selected.
  36. var sum = 0;
  37. $('input:checkbox:checked').each(function () {
  38. sum += Number(this.value);
  39. });
  40.  
  41. // Translate sum into an index (column #) into bools.
  42. var index = questions[0].bools.length - sum - 1;
  43.  
  44. // Clear the answers.
  45. $('#ul').html('');
  46.  
  47. // Add appropriate answers.
  48. for (var i = 0; i < answers.length; i++) {
  49. if (answers[i].bools[index]) {
  50. $('#ul').append('<li>' + answers[i].text + '</li>')
  51. }
  52. }
  53. });
  54. });
  55. </script>
  56. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement