Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding two variables together

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

like image 905
Jimmyt1988 Avatar asked Jun 16 '26 18:06

Jimmyt1988


1 Answers

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 (?)

like image 86
garph0 Avatar answered Jun 19 '26 09:06

garph0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!