3
I have a class activity to do, where I must receive from the user the amount of the purchase and the amount paid, and then display the change. So far ok, only it also asks to display in a textarea the notes used in the change, always informing as few notes as possible, example:
vlrCompra = 53,00
vlrPago = 100,00
vlrTroco = 47,00
notasTroco =
4 notas de 10,00
1 nota de 5,00
2 notas de 1,00
I have no idea how to do that last part, someone to help me? The notes I have are 1, 5, 10, 50
Here’s what I’ve done so far:
function calculaTroco(){
var valorCompra = parseFloat($("#valorCompra").val());
var valorPago = parseFloat($("#valorPago").val());
var valorTroco = 0;
if (valorPago == valorCompra){
valorTroco = 0;
$("#valorTroco").val(valorTroco);
alert("Não gerou troco");
}else if(valorPago > valorCompra){
valorTroco = valorPago - valorCompra;
$("#valorTroco").val(valorTroco);
}else{
alert("Não gerou troco (Valor pago menor que valor da compra)");
}
$("#valorCompra").val("");
$("#valorPago").val("");
$("#valorCompra").focus();
}
Remembering that I need to do this part of the notes in another Function, so far we have learned array, for...
Part of HTML:
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8"/>
<title>Calcular Troco</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<form id="formulario">
<fieldset>
<legend>Calcular Troco</legend>
<label for="valorCompra" >Valor da compra:</label>
<input type="text" id="valorCompra" name="valorCompra" /><br />
<label for="valorPago" >Valor pago:</label>
<input type="text" id="valorPago" name="valorPago" /><br /><br />
<button type="button" id="button" onclick="calculaTroco()">Calcular troco</button><br /><br />
<label for="valorTroco" >Valor do troco:</label>
<input type="text" id="valorTroco" name="valorTroco" readonly="readonly"/><br /><br />
<label for="notasUtilizadas">Notas utilizadas:</label>
<textarea rows="3" id="notasUtilizadas" readonly="readonly" ></textarea><br/>
</fieldset>
</form>
<script type="text/javascript" src="troco.js"></script>
</body>
</html>
I won’t do your exercise for you, but you’ll need to understand about remainder of division and entire division (you can truncate a normal division) to solve your problem.
– fernandosavio
fernandosavio could give me more tips for getting the exercise? I will need to use arrays?
– user156424
You can even use arrays, but it’s not part of your core problem. You need to learn what the division and its rest are for. You need to understand how you figure out how many X’s fit inside as many Y’s...
– fernandosavio