开发者

Sum rows in javascript (not a table)

开发者 https://www.devze.com 2023-04-03 12:26 出处:网络
I have an textarea for input of values that are rows and columns (but not a table) and I want to be able to add the values of the rows individually. My logic is that I get the user to input the number

I have an textarea for input of values that are rows and columns (but not a table) and I want to be able to add the values of the rows individually. My logic is that I get the user to input the number of rows they have entered, split the input to make a string and then split it up by the number of rows they input. Hereis what I have. Happy for better solutions. Alternately I did some reading and thought I could convert the rows to actual <tr>s and then go through them (I would also like to be able to this for columns at a later stage). Thanks in advance:

<html>
<head>
<script type='text/javascript'>

function sum(){ 
var rows= document.getElementById('rows').value;
    var val = document.getElementById('userInput').value;
    var temp = val.split(" ");
var lines=temp.split(rows);

var total = 0;
var v;

    for(var i = 0; i < temp.length; i++) {
      v = parseFloat(lines[i]);
      //this is what I am missing to get each row's sum separately
    } 




document.write(//each row total); 


  } 

</script>
</head>
<body>
<form id="input">
  <textarea id="userInput"></textarea> 
Number of rows: <textarea id="rows"></textarea>
  <input id="Run" type=Button value="run" onClick="sum()" />
</form>


</body>
</html> 

So now I have the following (I had to add v back in) but it is returning NaN (and noting I will address the final suggestion anyway):

<script type='text/javascript'>
 function sum() {
var grandTotal = 0,
   rowTotals = [], // array to hold individual row totals
   rowData,
   rows,
   val,
var v;
   rawData = document.getElementById('userInput').value;


 rows = rawData.split("\n");

 for (var i=0; i < rows.length; i++) {
  rowTotals[i] = 0;
  rowData = rows[i].split(" ");

  for (var j=0; j < rowData.length; j++) {
     val = parseFloat(rowData[j]);
    if (!isNa开发者_如何学PythonN(v)) rowTotals[i] += v;
  }
  alert("Total for row " + (i + 1) + ": " + rowTotals[i]);
  grandTotal += rowTotals[i];
 }

 alert("Grand total: " + grandTotal);
}


</script>


Assuming the user has entered data in this format:

12.1 4.8 11.2 4.1
1.2 3.4 5.6 99.9
etc

That is, with a new line at the end of each row and spaces between the numbers, then you could do something like this:

function sum() {
   var grandTotal = 0,
       rowTotals = [], // array to hold individual row totals
       rowData,
       rows,
       val,
       rawData = document.getElementById('userInput').value;

   // if desired replace this comment with regex validation of rawData

   rows = rawData.split("\n");

   for (var i=0; i < rows.length; i++) {
      rowTotals[i] = 0;
      rowData = rows[i].split(" ");
      // if you need to test that each row has the same number
      // of values in it do that here

      for (var j=0; j < rowData.length; j++) {
         val = parseFloat(rowData[j]);
         // add validation of val here
         rowTotals[i] += val;
      }
      alert("Total for row " + (i + 1) + ": " + rowTotals[i]);
      grandTotal += rowTotals[i];
   }

   // at this point rowTotals is an array containing
   // the individual row totals, so do what you like with that

   alert("Grand total: " + grandTotal);
}

There are two points in the function where you will need to do some more work:

  1. You must check that the result from parseFloat() is actually a number - the user may have entered alphabetic characters or punctuation, in which case it'll return NaN. Alternatively use a regular expression to validate the whole string for formatting and illegal characters before you do anything else.
  2. Splitting the string on newline "\n" can be a problem in that different platforms use different characters for line breaks. I believe some use just newline "\n", some use just carriage return "\r" and some use both. You could probably normalise that before the split using a find-and-replace.

I leave both points as an exercise for the reader...

0

精彩评论

暂无评论...
验证码 换一张
取 消