Untitled
unknown
plain_text
8 months ago
1.9 kB
5
Indexable
// Variables to store data
var balance = 0;
var transactions = [];
// Update balance and transactions on screen
function updateScreen() {
setText("balanceLabel", "Balance: $" + balance.toFixed(2));
setText("transactionList", formatTransactions());
}
// Format transactions for display
function formatTransactions() {
var formatted = "";
for (var i = 0; i < transactions.length; i++) {
formatted += transactions[i].type + ": $" + transactions[i].amount.toFixed(2) + "\n";
}
return formatted;
}
// Add Income Button
onEvent("addIncomeBtn", "click", function() {
var amount = getNumber("amountInput");
if (amount > 0) {
balance += amount;
transactions.push({type: "Income", amount: amount});
updateScreen();
setText("statusLabel", "Income added!");
} else {
setText("statusLabel", "Enter a valid amount.");
}
clearInput();
});
// Add Expense Button
onEvent("addExpenseBtn", "click", function() {
var amount = getNumber("amountInput");
if (amount > 0 && amount <= balance) {
balance -= amount;
transactions.push({type: "Expense", amount: amount});
updateScreen();
setText("statusLabel", "Expense added!");
} else {
setText("statusLabel", "Invalid amount or insufficient balance.");
}
clearInput();
});
// Delete Transaction Button
onEvent("deleteBtn", "click", function() {
if (transactions.length > 0) {
var lastTransaction = transactions.pop();
if (lastTransaction.type === "Income") {
balance -= lastTransaction.amount;
} else {
balance += lastTransaction.amount;
}
updateScreen();
setText("statusLabel", "Last transaction deleted!");
} else {
setText("statusLabel", "No transactions to delete.");
}
});
// Clear input field
function clearInput() {
setText("amountInput", "");
}
// Initial screen update
updateScreen();
Editor is loading...
Leave a Comment