Help to subtract 2 results

Asked

Viewed 82 times

2

Hello someone gives a hang here with this code , I would like to take the value of the input less the output subtract in the case, follow my code below !

<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: ID resultado_entries - ID resultado_exits

1 answer

1


In this case you can use the class to know which is and switch from positive to negative in a sum.

Something like that:

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: https://jsfiddle.net/kycym5wz/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.