0

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

1
  • remove else condition @Mumin Gazi Commented May 28, 2015 at 8:16

2 Answers 2

1

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);
      }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time @Bellash. i have tried your code i still have the same problem .. i have gave a demo of what iam doing i will glad if you help codepen.io/mumingazi/pen/XbNvBr
0

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

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.