truth tables

by Richard Hunter

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<table>
    <caption>OR Truth Table</caption>
    <thead>
        <tr>
            <th>input A</th>
            <th>input B</th>
            <th>output</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: orTruthTable">
        <tr>
            <td data-bind="text: inputA"></td>
            <td data-bind="text: inputB"></td>
            <td data-bind="text: output"></td>
        </tr>
    </tbody>
</table>


<table>
    <caption>AND Truth Table</caption>
    <thead>
        <tr>
            <th>input A</th>
            <th>input B</th>
            <th>output</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: AndTruthTable">
        <tr>
            <td data-bind="text: inputA"></td>
            <td data-bind="text: inputB"></td>
            <td data-bind="text: output"></td>
        </tr>
    </tbody>
</table>

<table>
    <caption>NOT Truth Table</caption>
    <thead>
        <tr>
            <th>input</th>
            <th>output</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: NotTruthTable">
        <tr>
            <td data-bind="text: input"></td>
            <td data-bind="text: output"></td>
        </tr>
    </tbody>
</table>

CSS

table {
    border-collapse : collapse;
    margin-bottom : 1rem;
    
}

th, td {
  
  padding: 0.25rem;
  text-align: left;
  border: 1px solid #ccc;
}

JavaScript

function AND(inputA, inputB) {

    return inputA && inputB;
}

function OR(inputA, inputB) {

    return inputA || inputB;
}

function NOT(input) {
    return !input;
}

var inputs = [
    { inputA : 0, inputB : 0 },
    { inputA : 1, inputB : 0 },
    { inputA : 0, inputB : 1 },
    { inputA : 1, inputB : 1 }
];

var orTruthTable = _.map(inputs, function (row) {

    return {
        inputA : row.inputA,
        inputB : row.inputB,
        output : OR(row.inputA, row.inputB)
    
    };    
});

var AndTruthTable = _.map(inputs, function(row) {
    return {
        inputA : row.inputA,
        inputB : row.inputB,
        output : AND(row.inputA, row.inputB)
    };

});

var NotTruthTable = _.map([1, 0], function (input) {
    return {
        input : input,
        output : +NOT(input)
    };
});

var model = {
        
    orTruthTable : orTruthTable,
    AndTruthTable : AndTruthTable,
    NotTruthTable : NotTruthTable
};


ko.applyBindings(model);