简体   繁体   English

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

[英]Sum two highest values using reverse() javascript

How do I sum the two highest values from a list of inputs using JavaScript? 如何使用JavaScript将输入列表中的两个最大值相加?

I've tried the below, but I get 4 instead of 5: 我尝试了以下方法,但得到的是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> 

First you find the two highest values, and then sum them together. 首先,找到两个最大值,然后将它们求和。 I'd probably do it something like this: 我可能会这样做:

 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"> 

More about that Array.prototype.map.call thing in this answer about looping through arrays and array-like things. 这个答案中有关Array.prototype.map.call的更多信息,涉及遍历数组和类似数组的事物。

But if you specifically want to use reverse , you'd do that after the sort : 但是,如果您特别想使用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"> 

You could try something like following: 您可以尝试如下操作:

 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); 
 <input class="compulsory1" type="text" value="1" /> <input class="compulsory1" type="text" value="3" /> <input class="compulsory1" type="text" value="2" /> 

Check this 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>

You can try something like this: 您可以尝试如下操作:

Steps 脚步

  • Get all elements 获取所有元素
  • Get their values. 获得他们的价值观。 Note, since you expect values to be number, yuo should parse them as well. 注意,由于您希望值是数字,因此yuo也应解析它们。
  • Sort value array in descending order instead of .sort + .reverse 以降序而不是.sort + .reverse对值数组进行排序
  • Calculate your sum 计算你的总和

Sample 样品

 // 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 ba }); var sum = cDatax[0] + cDatax[1]; console.log(sum) 
 <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> 

 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); 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM