1

How do I sum the two highest values from a list of inputs using JavaScript?

I've tried the below, but I get 4 instead of 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>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46

5 Answers5

1

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.

But if you specifically want to use reverse, you'd do that after the 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">
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • There is a problem with your code; It does not work correctly when value of ` ` is changed to ` ` Still value of three is sorted before 10 – Omari Victor Omosa Jul 29 '16 at 17:32
  • 1
    @krushiovida: Fixed. I'd forgotten that `Array#sort` defaults to a *string* sort, even if the data in the array is numbers, not strings. So we have to pass in our own comparison function no matter what we do. Which changes the first example above -- if we're going to pass our own function *anyway*, we may as well have it give us the results in a convenient order. – T.J. Crowder Jul 29 '16 at 17:47
1

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" />
Hitmands
  • 13,491
  • 4
  • 34
  • 69
1

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

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.
  • Sort value array in descending order instead of .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 b-a
});

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>
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
1

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>
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46