Mumin Gazi
Mumin Gazi

Reputation: 103

Dynamic input caluculation in JavaScript

Iam working on an app that requires me to calculate the SUM of a some fields.

i have my inputs are:

in1 = df[0][b];
in2 = df[0][c];
in3 = df[0][f];
total

Im pulling the contents as an array, the array could have a gap in between. for instance:

df[1][b] = 123; 
df[2][b] = empty or null; 
df[3][b] = 456;
df[4][b] = 5567;

the formula is to:

 df[0][f] = df[0][c] / df[0][b]; 
 total = sum of df[0][c]; 

i have tried the following code, it works fine but when i have GAPS (empty or null or NaN), the calculation stops! Any Ideas ?

  function updatesum()
    {
    var kon = 0;
    var lenArr = 0;
    for (i = 0; i < 1000; i++) {
    if(document.forms["sampleform"]["df["+i+"][c]"]) {lenArr++;}
    else { break; }
    }
    var total =0;
    for (j = 0; j < lenArr; j++) {
    total = total + (document.forms["sampleform"]["df["+j+"][c]"].value-0);
    document.forms["sampleform"]["df["+j+"][f]"].value = (document.forms["sampleform"]["df["+j+"][c]"].value-0) / (document.forms["sampleform"]["df["+j+"][b]"].value-0); 
    }
    document.forms["sampleform"]["toplam"].value = total;
    document.forms["sampleform"]["kalantutar"].value = total - (document.forms["sampleform"]["odenentutar"].value-0);
    }

Demo: http://codepen.io/mumingazi/pen/XbNvBr

Upvotes: 0

Views: 41

Answers (2)

Mumin Gazi
Mumin Gazi

Reputation: 103

I Have Solved the Problem:

function updatesum(ele)
{
 var kon = 0; 
 lenArr = new Array;

for (i = 0; i < 1000; i++) {
    if(document.forms["sampleform"]["df["+i+"][c]"]) {lenArr[i] = 1;}
    else {lenArr[i] = 0; }
}

var total =0;
     for (j = 0; j < lenArr.length; j++) {
if(lenArr[j] === 1){
total = total + (document.forms["sampleform"]["df["+j+"][c]"].value-0);
 document.forms["sampleform"]["df["+j+"][f]"].value = (document.forms["sampleform"]["df["+j+"][c]"].value-0) / (document.forms["sampleform"]["df["+j+"][b]"].value-0); 
}
}
document.forms["sampleform"]["toplam"].value = total;
document.forms["sampleform"]["kalantutar"].value = total - (document.forms["sampleform"]["odenentutar"].value-0);

 }

Upvotes: 0

Bellash
Bellash

Reputation: 8184

you need to use parseFloat or parseInt and then remove the break inside the if statement... You may use it like this

!isNAN(document.forms["sampleform"]["df["+j+"][f]"].value)? document.forms["sampleform"]["df["+j+"][f]"].value :0 or check if the value is a falsy one, then take 0 or 1 like this document.forms["sampleform"]["df["+j+"][f]"].value||0

Here is the complete source code

  function updatesum(){
      var kon = 0;
      var lenArr = 0;
      for (i = 0; i < 1000; i++) {
      if(document.forms["sampleform"]["df["+i+"][c]"]) {lenArr++;}
      //else { break; }
      }
      var total =0;
      for (j = 0; j < lenArr; j++) {
      total = total + (document.forms["sampleform"]["df["+j+"]      [c]"].value-0);
      document.forms["sampleform"]["df["+j+"][f]"].value = 
        (document.forms["sampleform"]["df["+j+"][c]"].value||0) / 
  (document.forms["sampleform"]["df["+j+"][b]"].value||1); 
      }
      document.forms["sampleform"]["toplam"].value = total;
      document.forms["sampleform"]["kalantutar"].value = 
        total - (document.forms["sampleform"]["odenentutar"].value||0);
      }

Upvotes: 1

Related Questions