0% found this document useful (0 votes)
42 views2 pages

Script Js

Uploaded by

tanmayntandel7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Script Js

Uploaded by

tanmayntandel7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Passwords for multiple users

const validPasswords = ["1234", "abcd", "pass2024", "mysecurepassword"];


let balance = 0;

const loginContainer = document.getElementById('login-container');


const appContainer = document.getElementById('app-container');
const loginMessage = document.getElementById('login-message');
const transactionHistory = document.getElementById('transaction-history');
const balanceDisplay = document.getElementById('balance');

// Login Function
function login() {
const password = document.getElementById('password').value;
if (validPasswords.includes(password)) {
loginContainer.classList.add('hidden');
appContainer.classList.remove('hidden');
loadTransactions();
} else {
loginMessage.innerText = "Invalid Password. Try again.";
loginMessage.style.color = "red";
}
}

// Logout Function
function logout() {
appContainer.classList.add('hidden');
loginContainer.classList.remove('hidden');
document.getElementById('password').value = ''; // Clear password field
}

// Add Transaction
function addTransaction() {
const amount = parseFloat(document.getElementById('amount').value);
const description = document.getElementById('description').value.trim();
const category = document.getElementById('category').value;

if (isNaN(amount) || description === '') {


alert('Please enter a valid amount and description.');
return;
}

// Update balance and display it


balance += amount;
balanceDisplay.innerText = `Total Balance: ₹${balance}`;

// Save transaction to localStorage


const transactions = JSON.parse(localStorage.getItem('transactions')) || [];
transactions.push({ amount, description, category });
localStorage.setItem('transactions', JSON.stringify(transactions));

// Display new transaction


displayTransaction({ amount, description, category });

// Clear input fields


document.getElementById('amount').value = '';
document.getElementById('description').value = '';
}

// Display a Transaction
function displayTransaction(transaction) {
const { amount, description, category } = transaction;
const li = document.createElement('li');
li.innerHTML = `${description} - <strong>${category}</strong> <span>${amount >=
0 ? '+' : ''}₹${amount}</span>`;
transactionHistory.appendChild(li);
}

// Load Transactions from localStorage


function loadTransactions() {
const transactions = JSON.parse(localStorage.getItem('transactions')) || [];
transactionHistory.innerHTML = ''; // Clear previous history
transactions.forEach(displayTransaction);

// Calculate and display total balance


balance = transactions.reduce((acc, t) => acc + t.amount, 0);
balanceDisplay.innerText = `Total Balance: ₹${balance}`;
}

You might also like