|
| 1 | +const balance = document.getElementById("balance"); |
| 2 | +const money_plus = document.getElementById("money-plus"); |
| 3 | +const money_minus = document.getElementById("money-minus"); |
| 4 | +const list = document.getElementById("list"); |
| 5 | +const form = document.getElementById("form"); |
| 6 | +const text = document.getElementById("text"); |
| 7 | +const amount = document.getElementById("amount"); |
| 8 | + |
| 9 | +const dummyTransactions = [ |
| 10 | + { id: 1, text: "Flower", amount: -20 }, |
| 11 | + { id: 2, text: "Salary", amount: 300 }, |
| 12 | + { id: 3, text: "Book", amount: -10 }, |
| 13 | + { id: 4, text: "Camera", amount: 150 } |
| 14 | +]; |
| 15 | + |
| 16 | +let transactions = dummyTransactions; |
| 17 | + |
| 18 | +// Add transaction to DOM list |
| 19 | +function addTransactionDOM(transaction) { |
| 20 | + // Get the sign |
| 21 | + const sign = transaction.amount < 0 ? "-" : "+"; |
| 22 | + |
| 23 | + const item = document.createElement("li"); |
| 24 | + |
| 25 | + // Add class based on value |
| 26 | + item.classList.add(transaction.amount < 0 ? "minus" : "plus"); |
| 27 | + |
| 28 | + item.innerHTML = ` |
| 29 | + ${transaction.text} <span>${sign}${Math.abs( |
| 30 | + transaction.amount |
| 31 | + )}</span><button class="delete-btn">X</button> |
| 32 | + `; |
| 33 | + |
| 34 | + list.appendChild(item); |
| 35 | +} |
| 36 | + |
| 37 | +// Init app |
| 38 | +function init() { |
| 39 | + list.innerHTML = ""; |
| 40 | + |
| 41 | + transactions.forEach(addTransactionDOM); |
| 42 | +} |
| 43 | + |
| 44 | +init(); |
0 commit comments