0
I need to create a script that makes the total purchase amount split and the split value appear in the Total Value field. I can’t identify what’s missing in my code
<form>
Valor da Compra <br>
<input type="text" id="valorCompra"><br>
Parcelas <br>
<select id="parcelas">
<option value="#">1x</option>
<option value="#">2x</option>
<option value="#">3x</option>
<option value="#">4x</option>
</select><br>
Valor Total <br>
<input type="text" id="valorTotal" readonly><br>
<button type="button" id="btn">Calcular</button>
</form>
<script>
var $valorCompra = document.getElementById('valorCompra');
var $valorTotal = document.getElementById('valorTotal');
var $parcelas = document.querySelectorAll('option');
var $btn = document.getElementById('btn');
var $vlrCompra = parseFloat($valorCompra.value);
var $parcel = parseFloat($parcelas.value);
var $msg = '';
var $valorFinal = 0;
function btn(){
$btn.addEventListener('click', function(){
calcular();
})
}
btn();
function calcular() {
if ($parcelas == 1) {
$valorFinal = $valorCompra;
$msg = 'O valor da compra é: ' + $valorFinal;
} else if ($parcelas == 2) {
$valorFinal = $valorCompra + ($valorCompra * 0.03);
$msg = 'O valor da compra é: ' + $valorCompra + ' em 2x de: ' + $valorFinal / 2;
} else if ($parcelas == 3) {
$valorFinal = $valorCompra + ($valorCompra * 0.05);
$msg = 'O valor da compra é: ' + $valorCompra + ' em 3x de: ' + $valorFinal / 3;
} else {
$valorFinal = $valorCompra + ($valorCompra * 0.07);
$msg = 'O valor da compra é: ' + $valorCompra + ' em 4x de: ' + $valorFinal / 4;
}
}
André Lins, thanks a lot for the help, it worked, I made the corrections, the main problems were the variables outside the function and how I had made the calculation, had other corrections. Thanks again.
– Thiago Pernomian