4
I was testing a script to calculate interest and realized that my code does not work on Google Chrome
, but if I open the same script on Mozilla
it works perfectly.
I wonder what it might be?
elementsArray = document.querySelectorAll('input');
elementsArray.forEach(function(elem) {
elem.addEventListener("input", function() {
calcula();
});
});
function calcula() {
vencimento = document.getElementById("datavenc").value;
pagamento = document.getElementById("datapag").value;
venc_array = vencimento.split("/");
vencimento = venc_array[2] + "-" + venc_array[1] + "-" + venc_array[0];
pagt_array = pagamento.split("/");
pagamento = pagt_array[2] + "-" + pagt_array[1] + "-" + pagt_array[0];
d1 = new Date(vencimento);
d2 = new Date(pagamento);
dias_atraso = parseInt((d2 - d1) / (24 * 3600 * 1000));
valortit = parseFloat(document.getElementById("valortitulo").value);
outrasdesp = document.getElementById("despesas").value;
outrasdesp = (outrasdesp == "") ? 0 : parseFloat(outrasdesp);
juros = ((valortit * .05) / 30) * (dias_atraso);
if (!isNaN(dias_atraso) && !isNaN(juros)) {
document.getElementById("diasematraso").value = dias_atraso;
document.getElementById("juros").value = juros.toFixed(2).replace(".", ",");
document.getElementById("valortotal").value = (valortit + juros + outrasdesp).toFixed(2).replace(".", ",");
}
}
<fieldset style="position:relative; height:200px; float:left; width:360px;">
<legend>Calcular Juros</legend>
<label>Data de Vencimento:</label>
<input id="datavenc" type="date" class="form-control" /><br />
<label>Data de Pagamento: </label>
<input id="datapag" type="date" class="form-control" /><br />
<label>Valor do Titulo: </label>
<input id="valortitulo" type="text" class="form-control" /><br />
<label>Outras Despesas: </label>
<input id="despesas" type="text" class="form-control" /><br />
<br />
<label>Dias em Atraso: </label>
<input id="diasematraso" type="text" class="form-control" disabled/><br />
<label>Juros: </label>
<input id="juros" type="text" class="form-control" disabled/><br />
<label>Valor Total a Pagar: </label>
<input id="valortotal" type="text" class="form-control" disabled/>
</fieldset>
Google Chrome
I removed my answer for the following reason: When testing on my Notebook using the Chromium browser, the code did not work, but when testing on Google Chrome it worked, stating the current date as due and the payment date 10 days later, with the value of
R$ 500
obtained the interest valueR$ 8,33
this certain ?– NoobSaibot
Yes, the script is to calculate interest of late slips.
– Smoke Rohden
@Smokerohden explained why his code doesn’t work on both browsers and changed the title of his question to get a little wider reaching more readers.
– Marconi