views:

146

answers:

4

I have been trying for... about 4 hours now lmao.

currentCalc returns 50 currentSum returns 0 when i alert them. Yet I cannot add them together with parseInt????

what am i doing wrong :'(

var identRow = $('tr.identRow');
identRow.each(function () {
    var getIdentClass = $(this).attr('class').split(' ').slice(1);
    $('tr.ohp' + getIdentClass + ' td.EURm').each(function (index) {
        var currentCalc = parseInt($(this).text().replace('.', ''), 10);
        var currentSum = $('tr.' + getIdentClass + ' td.totalEURm', this).text().replace('.', '');
        total = parseInt(currentCalc, 10) + parseInt(currentSum, 10);
        $('tr.' + getIdentClass + ' td.totalEURm').text(total);
        if (index == 6) {
            alert(total);
        }
    });
});

EDIT:

Oh goodness. Im completely confused now. I putr the break there. It says total = 50.

I want each iteration to add itself to the total. That is why I add currentCalc to the text of the field im plopping the currentCalc into.

$('tr.' + getIdentClass + ' td.totalEURm').text(total);

with my code now like this:

    var identRow = $('tr.identRow');
    identRow.each(function () {
      var getIdentClass = $(this).attr('class').split(' ').slice(1);
      $('tr.ohp' + getIdentClass + ' td.EURm').each(
        function (index) {
          var currentCalc = parseInt($(this).text().replace('.', ''), 10) || 0;
          var currentSum  = parseInt($('tr.' + getIdentClass + ' td.totalEURm', this).text().replace('.', ''), 10) || 0;
          var total = currentCalc + currentSum;
          $('tr.' + getIdentClass + ' td.totalEURm').text(total);
          if (index === 6) {
            alert(total);
          }
        });
    });

it alerts: 50, then 0, then 50, then 0.

EDIT:

How do I add currentCalc to its last value?

So first iteration its 10, seconds its 20. How do i make it so on the 2nd iteration it equals 30. currentCalc++ is just adding 1 to it.

Now you understand how crap i am :)

+1  A: 

I am no expert in JS, but I saw that currentCalc is already an int:

var currentCalc = parseInt($(this).text().replace('.',''), 10);
//...
total = parseInt(currentCalc, 10) + parseInt(currentSum, 10);

so probably the parseInt on an int instead that on a string fails (?)

garph0
I've just changed it so it now does not have parseInt. It still returns Nan. I cannot convert the text "50" and "0" into numbers.
James T
The only way in which I was able to get a NaN si by passing the string "O" (capital o) instead of "0" zero.Check that in your source you do not have capital O's instead of zeroes.
garph0
Cheers for the recommendation, but it really is 0 and 50. Infact, I copied them from the alert just to make sure. They are coming froma database you see, but are being converted to text and that's why i need them in number again to do my thaaang.
James T
This is strange. I could not produce NaN in any other way :)Could it be a browser's problem? Have you tried it in another browser, just to be sure?
garph0
Cheers for consistent help. If you follow the comments for my original question, we are getting somewhere there.. Any further ideas?
James T
Not really. The firebug comment above sounds good: it seems to me that something is going wrong with your .each iterations.Btw, if you just want to keep a running total you could also use a jscript var instead of writing the updated total into the table, but I suppose that's done on purpose.
garph0
A: 

I do some code formatting and cleanup, try this:


var identRow = $('tr.identRow');
identRow.each(function () {
  var getIdentClass = $(this).attr('class').split(' ').slice(1);
  $('tr.ohp' + getIdentClass + ' td.EURm').each(
    function (index) {
      var currentCalc = parseInt($(this).text().replace('.', ''), 10) || 0;
      var currentSum  = parseInt($('tr.' + getIdentClass + ' td.totalEURm', this).text().replace('.', ''), 10) || 0;
      var total = currentCalc + currentSum;
      $('tr.' + getIdentClass + ' td.totalEURm').text(total);
      if (index === 6) {
        alert(total);
      }
    });
});

I added condition if parseInt fails the vars currentCalc and currentSum will be 0. Also, like in answer above i'm avoiding double parseInt

Can you give an example html page to try out?

sbmaxx
Ok, unfortunately It is all locked up so I can't really, i'm sure id get in trouble if I showed u all.Thanks for the code changes there.Basically.. when I alert total, I get two alerts.first one alerts "50".Second alerts "0".And ofcourse, I want total to alert "50". because 50+0 is 50. Hope that helps.
James T
Probably you get two alerts because they come from two different iteractions: you nested two each, so if there is more than one tr.identRow, on each of these will be executed your second .each code.Probably the first row is ok and outputs 50, while the second outputs 0
garph0
I was confused when writing it tbh.I have 2 categories of data. they are represented in a table which is all categorised.My first each loop goes and grabs all iterations of the identRow. There are only 2, so it should iterate twice. Then it grabs the second class name from that object so that it then knows that the data in this category belongs to this category, and the data in the next belongs to the next. I then have two columns. It should iterate through the first category and add all data (numbers) and output into that category column. Then it moves onto the next category etc.
James T
+1  A: 

If you get two alerts, that likely means either your outer or inner .each statements is matching two entries.

If you're using firebug, use console.debug(total); instead of alert(). I recommend using console.debug(this) at some point to make sure it has what you think it has, too. Put it above the alert(). That information would be useful to see.

Sabrejack
A: 
    //SUM OF COLUMNS
    var total = 0;
    var identRow = $('tr.identRow');
    identRow.each(function () {
      var getIdentClass = $(this).attr('class').split(' ').slice(1);
      $('tr.ohp' + getIdentClass + ' td.EURm').each(
        function (index) {
            var currentCalc = $(this).text().replace('.', '');
            total = parseInt(currentCalc)+total;
            $('tr.' + getIdentClass + ' td.totalEURm').text(total);
        });
    });

did the trick.

Now i just gotta set the total to 0 when it gets to the second category because at the moment it keeps adding from where it left off. Progress though. Thanks for everything

James T