Jump to content

Decision tables: Difference between revisions

Added JavaScript
(Added JavaScript)
Line 536:
 
For large numbers of rules and few actions, J's native support of sparse arrays might provide a performance advantage, particularly in space. A minor note about the implementation here: the verb (function) <tt>troubleshoot</tt> is generalized, and reusable on any set of rule table, questions, and suggested actions. The default is to use those given in the printer troubleshooting table.
 
 
 
=={{header|JavaScript}}==
Since this is an interactive web page, results can't be listed. See http://jsfiddle.net/rGP6C/1/ for a live demo.
<lang javascript><html>
<head></head>
<body>
<form id="form"></form>
<br/>Possible solutions:
<ul id="ul"></ul>
</body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
var questions = [
{ bools:[1,1,1,1,0,0,0,0], text:"Printer does not print" },
{ bools:[1,1,0,0,1,1,0,0], text:"A red light is flashing" },
{ bools:[1,0,1,0,1,0,1,0], text:"Printer is unrecognized" },
];
var answers = [
{ bools:[0,0,1,0,0,0,0,0], text:"Check the power cable" },
{ bools:[1,0,1,0,0,0,0,0], text:"Check the printer-computer cable" },
{ bools:[1,0,1,0,1,0,1,0], text:"Ensure printer software is installed" },
{ bools:[1,1,0,0,1,1,0,0], text:"Check/replace ink" },
{ bools:[0,1,0,1,0,0,0,0], text:"Check for paper jam" },
]
 
$(document).ready(function() {
// Init form with questions. "Value" is a descending power of 2.
var value = questions[0].bools.length;
for (var i = 0; i < questions.length; i++) {
value /= 2;
var el = '<br /><input type="checkbox" value="' + value + '">' + questions[i].text;
$("#form").append(el);
}
 
// Respond to a checkbox action.
$('input:checkbox').change(function() {
 
// Figure out which combination of checkboxes the user selected.
var sum = 0;
$('input:checkbox:checked').each(function () {
sum += Number(this.value);
});
 
// Translate sum into an index (column #) into bools.
var index = questions[0].bools.length - sum - 1;
 
// Clear the answers.
$('#ul').html('');
 
// Add appropriate answers.
for (var i = 0; i < answers.length; i++) {
if (answers[i].bools[index]) {
$('#ul').append('<li>' + answers[i].text + '</li>')
}
}
});
});
</script>
</html></lang>
 
 
 
=={{header|Perl 6}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.