-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (36 loc) · 1.12 KB
/
script.js
File metadata and controls
46 lines (36 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const form = document.getElementById('formGasto');
const descricaoInput = document.getElementById('descricao');
const valorInput = document.getElementById('valor');
const lista = document.getElementById('listaGastos');
const totalSpan = document.getElementById('total');
let gastos = JSON.parse(localStorage.getItem('gastos')) || [];
function render() {
lista.innerHTML = '';
let total = 0;
gastos.forEach((gasto, index) => {
total += gasto.valor;
const li = document.createElement('li');
li.innerHTML = `
<span>${gasto.descricao} - R$ ${gasto.valor.toFixed(2)}</span>
<button onclick="remover(${index})">❌</button>
`;
lista.appendChild(li);
});
totalSpan.textContent = total.toFixed(2);
localStorage.setItem('gastos', JSON.stringify(gastos));
}
function adicionar(descricao, valor) {
gastos.push({ descricao, valor });
render();
}
function remover(index) {
gastos.splice(index, 1);
render();
}
form.addEventListener('submit', e => {
e.preventDefault();
adicionar(descricaoInput.value, Number(valorInput.value));
descricaoInput.value = '';
valorInput.value = '';
});
render();