IP vs Mask

by nekyouto

HTML

<!-- stitch this with the module declare in Javascript -->
<body ng-app="myApp">
    <div ng-controller="SelectCtrl">
        <table>
            <tr>
                <td>IP Address:</td>
            </tr>
            <tr>
                <td>
                    <input type="text" ng-model="ip[0]"/>.
                    <input type="text" ng-model="ip[1]"/>.
                    <input type="text" ng-model="ip[2]"/>.
                    <input type="text" ng-model="ip[3]"/>
                </td>
            </tr>
            <tr>
                <td>Subnet Mask:</td>
            </tr>
            <tr>
                <td>
                    <input type="text" ng-model="mask[0]"/>.
                    <input type="text" ng-model="mask[1]"/>.
                    <input type="text" ng-model="mask[2]"/>.
                    <input type="text" ng-model="mask[3]"/>
                </td>
            </tr>
            <tr>
                <td><button ng-click="checkNow();">Check Now</button></td>
            </tr>
            <tr>
                <td>{{result}}</td>
            </tr>
        </table>
    </div>
</body>

CSS

table {
    width:100%;
}
tr {
    width:100%;
}
td {
    width:100%;
    text-align:center;
}

JavaScript

// declare a module
var myApp = this.angular.module('myApp', []);

// declare a controller
myApp.controller('SelectCtrl', function ($scope) {
    $scope.ip = [0, 0, 0, 0];
    $scope.mask = [0, 0, 0, 0];
    $scope.checkNow = function () {
        $scope.result = compareIpMaskNetwork($scope.ip, $scope.mask);
    }
});

function compareIpMaskNetwork(ip, mask){
    var ipStr = convertValueto8Bits(ip);
    var maskStr = convertValueto8Bits(mask);
    var ipStrArr = ipStr.split("");
    var maskStrArr = maskStr.split("");
    var result = "";
    var curr = "";
    for(var i = 0; i < maskStrArr.length; i ++){
        if(maskStrArr[i] == "1"){
            if(curr == ""){
                curr = ipStrArr[i];
                result = ipStrArr[i];
            }else{
                if(ipStrArr[i] != curr){
                    result = "2";
                    break;
                }
            }
        }else{
            break;
        }
    }
    return result;
}

function convertValueto8Bits(value){
    var str = "";
    for(var i = 0; i < value.length; i ++){
        str += convertIPto8Bits(value[i]);
    }
    return str;
}

function convertIPto8Bits(value){
    var val = Number(value);
    var str = "";
    if(val >= 128){
        val -= 128;
        str += "1";
    }else{
        str += "0";
    }
    if(val >= 64){
        val -= 64;
        str += "1";
    }else{
        str += "0";
    }
    if(val >= 32){
        val -= 32;
        str += "1";
    }else{
        str += "0";
    }
    if(val >= 16){
        val -= 16;
        str += "1";
    }else{
        str += "0";
    }
    if(val >= 8){
        val -= 8;
        str += "1";
    }else{
        str += "0";
    }
    if(val >= 4){
        val -= 4;
        str += "1";
    }else{
        str += "0";
    }
    if(val >= 2){
        val -= 2;
        str += "1";
    }else{
        str += "0";
    }
    if(val >= 1){
        val -= 1;
        str += "1";
    }else{
        str += "0";
    }
    return str;
}