gpt4 book ai didi

javascript - [jQuery/javascript] : find a couple of items (2) in an array

转载 作者:行者123 更新时间:2023-12-02 16:03:56 25 4
gpt4 key购买 nike

我有 10 行数字,每行有 5 个数字,用点分隔,如下所示:

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. 循环所有 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>

关于javascript - [jQuery/javascript] : find a couple of items (2) in an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com