Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Implement unique continuous ranking

I have 4 input fields to let user input the importance(rankings) of these 4 companies in a specific Area, as shown below :

<table>
<tr><th></th><th>Area 1</th></tr>
<tr><td>Company A</td><td><input type="text" name="text1" id="text1"></td></tr>
<tr><td>Company B</td><td><input type="text" name="text2" id="text2"></td></tr>
<tr><td>Company C</td><td><input type="text" name="text3" id="text3"></td></tr>
<tr><td>Company D</td><td><input type="text" name="text4" id="text4"></td></tr>
</table>

The input fields are not required and should be integer from 1 to 4(suppose there are NO duplicates),

user can not jump ranking which means the ranking need to be unique and continuous.

For example, *We can input 1,3,2 as well as 4,2,1,3.

But when the input is like 3,1, we then need to alert 2 is missing.

When we input 3, we need to alert 1 and 2 both missing.

Here is my code to check the ranking:

function checkMissingRank(object){
  object.change(function() {
    var max = 0;
    var actSum = 0;
    var rows = object.length;

    for(var i=1 ; i<=rows ; i++){
        if($('#text'+i+'').val() != ""){
            var actVal = parseInt($('#text'+i+'').val());

            //The actual sum of the values
            actSum = actSum + actVal;
            if(actVal>max){
                //Use the max to calculate the total sum should be
                max=actVal;
            }
        }
    }

    //The total sum should be 
    totalSum = ((1+max)*max)/2;

    //The difference is the missing value
    var missVal = totalSum - actSum;

    if(missVal != 0){
        alert("Ranking "+missVal+" is missing.");
    }
  });
}
checkMissingRank($('input[name^="text"]'));

It works fine when just one value is missing, but when there are 2 value missing (1, 2), it just return the sum(3) of them. How do I improve on this?

like image 239
user3368506 Avatar asked Mar 29 '16 08:03

user3368506


2 Answers

You can do this to check if there is a missing number in an array (original code from Dasari Srinivas) :

function foo(num) {
    if (num.length == 0) {
        return 0;
    }
    var set = [];
    var res = [];
    var max = 1;

    for (var i = 0; i < num.length; i++)
        set.push(num[i]);

    for (var i = 0; i < num.length; i++) {
        var left = num[i] - 1;
        var right = num[i] + 1;
        var count = 1;

        while (set.indexOf(left) != -1) {
            count++;
            set.splice(set.indexOf(left), 1);
            left--;
            res["min"] = left;
        }
        while (set.indexOf(right) != -1) {
            count++;
            set.splice(set.indexOf(right), 1);
            res["max"] = right;
            right++;
        }
        max = Math.max(count, max);
    }
    console.log(res);
    return max;
}

var array2 = [4, 1, 2, 3];
if (foo(array2) == array2.length) {
    alert("no missing");
} else {
    alert("missing");
}

array2 = [5, 1, 2, 3];
if (foo(array2) == array2.length) {
    alert("no missing");
} else {
    alert("missing");
}
<p>first array =>  [4, 1, 2, 3];</p>
<p>second array => [5, 1, 2, 3];</p>
like image 153
Quentin Roger Avatar answered Oct 16 '22 12:10

Quentin Roger


Am using a different logic here. Just coded it without compiling and checking it for errors. However, the logic should work in your scenario.

function checkMissingRank(object){
  object.change(function() {
    var max = 0;
    var actSum = 0;
    var rows = object.length;
    var missingVal = ["1", "2", "3", "4"];

    for(var i=1 ; i<=rows ; i++){
        if($('#text'+i+'').val() != ""){
            var actVal = $('#text'+i+'').val();

            if( missingVal.indexOf(actVal) !== -1){
                var index = missingVal.indexOf(actVal);
                missingVal.splice(index, 1);
            }


        }
    }

    if(missingVal.length > 1){ 
        var alertMsg = "Ranking ";
        for(var i = 0; i < missingVal.length; i++){
            alertMsg += missingVal[i];
            if( i < (missingVal.length -1) ){
                  alertMsg += ",";
            }   

        }
        alertMsg += " is missing.";

        alert(alertMsg);
    }
  });
}
checkMissingRank($('input[name^="text"]'));
like image 1
Luke P. Issac Avatar answered Oct 16 '22 13:10

Luke P. Issac