使用reverse()javascript对两个最高值求和

时间:2016-07-29 09:59:49

标签: javascript arrays sorting reverse

如何使用JavaScript对输入列表中的两个最高值求和?

我已尝试过以下内容,但我得到的是4而不是5:

<table id="tableID">
<tr>
<td>   <input name="name" class="compulsory1" type="text" value="1" />  </td>
<td>   <input name="name1" class="compulsory1" type="text" value="3" />  </td>
<td>   <input name="name2" class="compulsory1" type="text" value="2" />  </td>

<td>
<script type="text/javascript">
var tdsCompulsory = document.getElementsByClassName('compulsory1');
var len = tdsCompulsory.length;
var cDatax = [];
var cData = cDatax.reverse();
sum = 0;
for(var i = 0; i < 2; i++){
    cData.push(tdsCompulsory[i].value); 
    sum += +tdsCompulsory[i].value; 
}
alert (sum);

</script>

</td>

</tr>
</table>

5 个答案:

答案 0 :(得分:1)

首先,您找到两个最高值,然后将它们相加。我可能会这样做:

document.getElementById("the-button").onclick = function() {
    // Get the values as numbers
    var values = Array.prototype.map.call(
        document.getElementsByClassName('compulsory1'),
        function(input) {
            return +input.value; // + converts to number
        }
    );

    // Put them in order *highest* to *lowest*
    values.sort(function(left, right) {
        return right - left;
    });

    // Add the first two
    var result = values[0] + values[1];
    console.log(values[0] + " + " + values[1] + " = " + result);
};
<input name="name" class="compulsory1" type="text" value="1" />
<input name="name1" class="compulsory1" type="text" value="3" />
<input name="name2" class="compulsory1" type="text" value="2" />
<input id="the-button" type="button" value="Run">

有关this answerArray.prototype.map.call关于循环数组和数组类似事物的更多信息。

但如果您特别想使用reverse,那么您应该在sort之后执行此操作:

document.getElementById("the-button").onclick = function() {
    // Get the values as numbers
    var values = Array.prototype.map.call(
        document.getElementsByClassName('compulsory1'),
        function(input) {
            return +input.value; // + converts to number
        }
    );

    // Put them in order lowest to highest
    values.sort(function(left, right) {
        return left - right;
    });

    // Then reverse that
    values.reverse();

    // Add the first two
    var result = values[0] + values[1];
    console.log(values[0] + " + " + values[1] + " = " + result);
};
<input name="name" class="compulsory1" type="text" value="1" />
<input name="name1" class="compulsory1" type="text" value="3" />
<input name="name2" class="compulsory1" type="text" value="2" />
<input id="the-button" type="button" value="Run">

答案 1 :(得分:1)

您可以尝试以下内容:

&#13;
&#13;
function PickTwoHighestCtrl() {
  let els = document.querySelectorAll(".compulsory1");
  
  let values = Array.prototype.map.call(els, (el) => {
    return Number(el.value) || 0;
  });
  
  
  let highest = Math.max.apply(Math, values);
  
  let secondHighest = Math.max.apply(
    Math, values.filter(e => e !== highest)
  );
  
  console.log("Two highest values are:", highest, secondHighest, "and their sum is", highest + secondHighest)
}

document.addEventListener("DOMContentLoaded", PickTwoHighestCtrl);
&#13;
<input class="compulsory1" type="text" value="1" />
<input class="compulsory1" type="text" value="3" />
<input class="compulsory1" type="text" value="2" />
&#13;
&#13;
&#13;

答案 2 :(得分:1)

选中此fiddle

<html>
    <head>
    <script>
    var tdsCompulsory = document.getElementsByClassName('compulsory1');
    var cDatax = [];
    sum = 0;
    for(var i = 0; i < 3; i++){
        cDatax.push(tdsCompulsory[i].value); 
     }

    // let's convert it to a real array of numbers, not of strings :
    var intArray = cDatax.map(Number);
    var max = Math.max.apply(null, intArray);
    // now let's sort it and take the second element :
    var second = intArray.sort(function(a,b){return b-a})[1];
    var sum = max+second;
    alert(sum)
    </script>
    </head>
    <body>
    <table id="tableID">
    <tr>
    <td>   <input name="name" class="compulsory1" type="text" value="1" />  </td>
    <td>   <input name="name1" class="compulsory1" type="text" value="3" />  </td>
    <td>   <input name="name2" class="compulsory1" type="text" value="2" />  </td>

    <td>


    </td>

    </tr>
    </table>
    </body>
    </html>

答案 3 :(得分:1)

您可以尝试这样的事情:

步骤

  • 获取所有元素
  • 获取他们的价值观。请注意,因为您希望值为数字,所以yuo也应该解析它们。
  • 按降序排序值数组,而不是.sort + .reverse
  • 计算您的总和

示例

&#13;
&#13;
// Get all elements
var tdsCompulsory = document.getElementsByClassName('compulsory1');

// Get values from all elements
var valArr = Array.prototype.map.call(tdsCompulsory, function(el) {
  return parseInt(el.value)
});

// Sort value array in descending order
var cDatax = valArr.sort(function(a, b) {
  return b-a
});

var sum = cDatax[0] + cDatax[1];
console.log(sum)
&#13;
<table id="tableID">
  <tr>
    <td>
      <input name="name" class="compulsory1" type="text" value="1" />
    </td>
    <td>
      <input name="name1" class="compulsory1" type="text" value="3" />
    </td>
    <td>
      <input name="name2" class="compulsory1" type="text" value="2" />
    </td>
  </tr>
</table>
&#13;
&#13;
&#13;

答案 4 :(得分:1)

&#13;
&#13;
var tdsCompulsory = document.getElementsByClassName('compulsory1'),
    cDatax = Array.prototype.map.call(tdsCompulsory, function(el) {
        return parseInt(el.value) || 0;
    }),
    sum = 0;

cDatax
  .sort(function(a, b){
    return a - b;
  })
  .reverse();


sum = cDatax[0] + cDatax[1];
console.log(sum);
&#13;
<table id="tableID">
  <tr>
    <td><input name="name" class="compulsory1" type="text" value="1" /></td>
    <td><input name="name1" class="compulsory1" type="text" value="3" /></td>
    <td><input name="name2" class="compulsory1" type="text" value="2" /></td>
  </tr>
</table>
&#13;
&#13;
&#13;