2015-06-20 23 views
0

我有10行号码找到几个项目(2)的,每行有5号由点分开,例如:[jQuery的/ JavaScript的]:以阵列

enter image description here

所有那些与同一个类名称连续。

我现在必须在每一行中找到它们是否合计在一起90。

因此,如果第一行包含“2”和“88”(以任何顺序),它们会突出显示(数字,而不是行)。与第二行,第三个等等一样。

目的是找到2个数字总和为90(只有两个,而不是3个或更多)。

我目前在此:

function CheckAndHighlight(className){ 
     var numebrsTd = $("td[class*="+className+"]"); 
     var numbers = numebrsTd.html().split('.'); 

     for(var i=0;i<numbers.length;i++) 
      { 
      var numI=parseInt(numbers[i],10); 
      for(var j=0;j<numbers.length;j++) 
       { 
        var numJ=parseInt(numbers[j],10); 

        // Avoid summing up numbers at same index 
        if(i==j) continue; 

        // Check if two numbers sum up to 90 
        if(numI+numJ == 90) 
        { 
         // Clear the row 
         numebrsTd.html(''); 

         // Loop through all IP numbers of the row 
         for(var k=0;k<numbers.length;k++) 
         { 
          var numK=parseInt(numbers[k],10); 
          // Create span with th number 
          var span = $('<span>'+numK+'</span>') 
          // Check the index with index of numbers to be highlighted 
          var highlight = k == i || k == j; 
          // Add class to the span if to highlight 
          if(highlight) span.addClass('highlight'); 
          // add '.' at the end of number if not last one 
          if(k<numbers.length) span.append('.'); 
          // Add new Span to the row 
          numebrsTd.append(span); 
         } 

         return;     
        } 
       } 
      } 
    } 

查看当前的工作:http://lt.mrweb.info/

我需要的功能,工作只是通过调用它CheckAndHighlight('bari')CheckAndHighlight('cagliari')

+1

如何是你的问题相关的jQuery?这只是JavaScript。 – Greendrake

回答

1

试试这个。

下面的代码工作如下:

  1. 遍历所有IP行
  2. 提取IP号码由分裂行“”
  3. 从第一号开始尝试通过一个与数字一个休息总结检查90
  4. 如果两个数字使90和再由IP行中使用它们的索引突出显示这些数字,并得到了循环

function ProcessRowsWithClass(className) 
 
{ 
 
    $('td[class*='+className+']').each(function(){ 
 
    CheckAndHighlight($(this)) 
 
    }); 
 
} 
 

 
function CheckAndHighlight(numebrsTd) 
 
{ 
 
    var numbers = numebrsTd.html().split('.'); 
 
    for(var i=0;i<numbers.length;i++) 
 
     { 
 
     var numI=parseInt(numbers[i],10); 
 
     for(var j=0;j<numbers.length;j++) 
 
      { 
 
       var numJ=parseInt(numbers[j],10); 
 
       
 
       // Avoid summing up numbers at same index 
 
       if(i==j) continue; 
 
      
 
       // Check if two numbers sum up to 90 
 
       if(numI+numJ == 90) 
 
       { 
 
        // Clear the row 
 
        numebrsTd.html(''); 
 
        
 
        // Loop through all IP numbers of the row 
 
        for(var k=0;k<numbers.length;k++) 
 
        { 
 
         var numK=parseInt(numbers[k],10); 
 
         // Create span with th number 
 
         var span = $('<span>'+numK+'</span>') 
 
         // Check the index with index of numbers to be highlighted 
 
         var highlight = k == i || k == j; 
 
         // Add class to the span if to highlight 
 
         if(highlight) span.addClass('highlight'); 
 
         // add '.' at the end of number if not last one 
 
         if(k<numbers.length) span.append('.'); 
 
         // Add new Span to the row 
 
         numebrsTd.append(span); 
 
        } 
 
        
 
        return;     
 
       } 
 
      } 
 
     } 
 
} 
 

 
ProcessRowsWithClass('genova'); 
 
ProcessRowsWithClass('bari'); 
 
ProcessRowsWithClass('cagliari');
span.highlight{ 
 
    color:green; 
 
    font-weight:bold; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h1>Bari</h1> 
 
<table> 
 
<tr><td class="bari1">80.10.86.30.65</td></tr> 
 
<tr><td class="bari2">96.11.73.36.13</td></tr> 
 
<tr><td class="bari3">78.34.50.72.40</td></tr> 
 
<tr><td class="bari4">34.78.69.60.22</td></tr> 
 
<tr><td class="bari5">12.29.30.69.33</td></tr> 
 
<tr><td class="bari6">70.10.20.70.44</td></tr> 
 

 
</table> 
 
<h1>Cagliari</h1> 
 
<table> 
 
<tr><td class="cagliari1">80.10.86.30.65</td></tr> 
 
<tr><td class="cagliari2">96.11.73.36.13</td></tr> 
 
<tr><td class="cagliari3">78.34.50.72.40</td></tr> 
 
<tr><td class="cagliari4">34.78.69.60.22</td></tr> 
 
<tr><td class="cagliari5">12.29.30.69.33</td></tr> 
 
<tr><td class="cagliari6">70.10.20.70.44</td></tr> 
 

 
</table> 
 

 
<h1>Genova</h1> 
 
<table> 
 

 
<tr><td class="genova1">80.10.86.30.65</td></tr> 
 
<tr><td class="genova2">96.11.73.36.13</td></tr> 
 
<tr><td class="genova3">78.34.50.72.40</td></tr> 
 
<tr><td class="genova4">34.78.69.60.22</td></tr> 
 
<tr><td class="genova5">12.29.30.69.33</td></tr> 
 
<tr><td class="genova6">70.10.20.70.44</td></tr> 
 

 
</table>

1

分裂您行转换为数字以检查它们中的任何两个是否可以使您的目标总和为90.以下示例在给出的五行中输出“1.34.5.89.1”和“3.47.12.43.3”:

var rows = [ 
 
     '1.2.23.34.5', 
 
     '1.34.5.89.1', 
 
     '75.44.5.89.5', 
 
     '5.3.2.55.6', 
 
     '3.47.12.43.3' 
 
    ], 
 
    check = function(row, target) { 
 
     row = row.split('.'); 
 
     var i = 0, 
 
      j, 
 
      l = row.length; 
 
     for (; i < l-1; i++) { 
 
      for (j = i+1; j < l; j++) { 
 
       if (parseInt(row[i], 10) + parseInt(row[j], 10) === target) { 
 
        return true; 
 
       } 
 
      } 
 
     } 
 
     return false; 
 
    }, 
 
    i = 0, 
 
    target = 90; 
 
for (; i < rows.length; i++) { 
 
    if (check(rows[i], target)) { 
 
     console.log(rows[i]); 
 
    } 
 
}

+0

Tks!请参阅我的编辑。 (ps:代码片段不工作) –