Created
January 6, 2015 02:26
-
-
Save barenko/fe2120d50a02c1d89d3f to your computer and use it in GitHub Desktop.
Simulação de cálculo de financiamento utilizando tabela SAC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<article> | |
<header>Cálculo de financiamento</header> | |
<form> | |
<label>SAC <input id="sac">% </label> | |
<br> | |
<label>Valor Financiado R$ <input id="valorFinanciado" type="number" min="1"> </label> | |
<br> | |
<label>Total de meses <input id="totalMeses" type="number" min="1"> </label> | |
<br> | |
<button id="calcular">Calcular</button> | |
</form> | |
</article> | |
<section id="resultado"> | |
<label> Amortização <input id="amortizacao" readonly="readonly"> </label> | |
<article> | |
<table> | |
<thead> | |
<tr> | |
<th>Valor de juros: </th> | |
<th id="valorDosJuros"></th> | |
</tr> | |
<tr> | |
<th>Valor total: </th> | |
<th id="valorTotal"></th> | |
</tr> | |
<tr> | |
<th>Número da parcela</th> | |
<th>Valor da parcela</th> | |
</tr> | |
</thead> | |
<tbody id="parcelas"> | |
</tbody> | |
</table> | |
</article> | |
</section> | |
<script> | |
function calculoSAC(percentagemSAC, totalMeses, valorFinanciado){ | |
var parcelas = [] | |
var amortizacao = valorFinanciado/totalMeses | |
for(var i=0;i<totalMeses;i++){ | |
var valor = amortizacao + (percentagemSAC/100) * (valorFinanciado - i * amortizacao) | |
parcelas.push([i+1, valor]) | |
} | |
return parcelas | |
} | |
function calculoTotal(){ | |
var parcelas = document.getElementsByName('valorParcela') | |
var total = 0 | |
for(var i=0; i<parcelas.length;i++){ | |
total += parseFloat(parcelas[i].textContent) | |
} | |
return total | |
} | |
function calcular(event){ | |
var sac = parseFloat(document.getElementById('sac').value) | |
var valorFinanciado = parseFloat(document.getElementById('valorFinanciado').value) | |
var totalMeses = parseFloat(document.getElementById('totalMeses').value) | |
var amortizacao = valorFinanciado/totalMeses | |
document.getElementById('amortizacao').value = amortizacao || '' | |
var grid = calculoSAC(sac, totalMeses, valorFinanciado) | |
var parcelas = document.getElementById('parcelas') | |
parcelas.innerHTML = '' | |
for(var i=0; i<grid.length; i++){ | |
var numeroParcela = document.createElement("td") | |
numeroParcela.innerHTML = grid[i][0] | |
var valorParcela = document.createElement("td") | |
valorParcela.setAttribute("name", "valorParcela") | |
valorParcela.innerHTML = grid[i][1] | |
var linha = document.createElement("tr") | |
linha.appendChild(numeroParcela) | |
linha.appendChild(valorParcela) | |
parcelas.appendChild(linha) | |
} | |
var total = calculoTotal() | |
document.getElementById('valorTotal').innerHTML = total | |
document.getElementById('valorDosJuros').innerHTML = total-valorFinanciado | |
event.preventDefault(); | |
} | |
document.getElementById('calcular').addEventListener('click', calcular, false); | |
</script> | |
<body> | |
<html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment