Help to subtract 2 results

2

Hello, I'm not sure what to do, but I do not know what to do.

<script src="jquery_somar.js"></script>

<!--  1 Soma das Entradas -->  

Soma das Entradas 

<div class="Entradas">100,00</div>
<div class="Entradas">100,00</div>
<br>
<div id="resultado_entradas"></div>


<script type="text/javascript">

var total = $('.Entradas').get().reduce(function(tot, el) {
    var numero = el.innerHTML.split('.').join('').split(',').join('.');
    return tot + Number(numero);
}, 0);
$('#resultado_entradas').html(total.toLocaleString(undefined, {minimumFractionDigits: 2}));
    </script>


<!--  2 Soma das Saidas -->   


<div class="Saidas">10,00</div>
<div class="Saidas">10,00</div>
<div id="resultado_saidas"></div>   




    <script type="text/javascript">

var total = $('.Saidas').get().reduce(function(tot, el) {
    var numero = el.innerHTML.split('.').join('').split(',').join('.');
    return tot + Number(numero);
}, 0);
$('#resultado_saidas').html(total.toLocaleString(undefined, {minimumFractionDigits: 2}));
    </script>

Total: Input_ID_ID - Output_ID

    
asked by anonymous 14.08.2016 / 15:52

1 answer

1

In this case you can use the class to know what it is and change from positive to negative in a sum.

Something like this:

var total = $('.Saidas, .Entradas').get().reduce(function(tot, el) {
    var numero = Number(el.innerHTML.split('.').join('').split(',').join('.'));
    return tot + (el.classList.contains('Entradas') ? numero : -numero);
}, 0);

jsFiddle: link

    
14.08.2016 / 17:24