JQuery - Introducing Unique Continuous Ranking

I have 4 input fields that allow the user to enter the value (rating) 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> 

Input fields are not required and must be integers from 1 to 4 (suppose there are no duplicates),

the user cannot jump in the rating, which means that the rating must be unique and continuous.

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

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

When we enter 3 , we need to warn 1 and 2 as missing.

Here is my code to check the rating:

 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 great when only one value is missing, but when 2 values ​​are missing ( 1 , 2 ), it simply returns the sum (3) of them. How can I improve this?

+7
javascript jquery html
source share
2 answers

You can do this to check if there is a missing number in the array (source 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> 
+1
source share

Different logic is used here. Just encoded it without compiling it and checking 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"]')); 
+1
source share

All Articles