Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added expense splitter extension #2615

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Expense Splitter/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Expense Splitter</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Expense Splitter</h1>
<div id="inputs">
<div class="input-group">
<input type="text" placeholder="Name" class="name">
<input type="number" placeholder="Amount Paid" class="amount">
<button class="remove">Remove</button>
</div>
</div>
<button id="add">Add Person</button>
<button id="calculate">Calculate</button>
<div id="results"></div>
</div>
<script src="script.js"></script>
</body>
</html>

19 changes: 19 additions & 0 deletions Expense Splitter/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"manifest_version": 3,
"name": "Expense Splitter",
"version": "1.0",
"optional_permissions" : ["tabs"],
"action": {
"default_popup": "index.html"
},
"web_accessible_resources": [
{
"resources": [
"index.html"
],
"matches": [
"<all_urls>"
]
}
]
}
81 changes: 81 additions & 0 deletions Expense Splitter/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
document.getElementById('add').addEventListener('click', function() {
addPerson();
});

document.getElementById('calculate').addEventListener('click', function() {
calculateExpenses();
});

document.getElementById('inputs').addEventListener('click', function(event) {
if (event.target.classList.contains('remove')) {
event.target.parentElement.remove();
}
});

function addPerson() {
const inputsDiv = document.getElementById('inputs');
const div = document.createElement('div');
div.className = 'input-group';
div.innerHTML = `
<input type="text" placeholder="Name" class="name">
<input type="number" placeholder="Amount Paid" class="amount">
<button class="remove">Remove</button>
`;
inputsDiv.appendChild(div);
}

function calculateExpenses() {
const names = document.querySelectorAll('.name');
const amounts = document.querySelectorAll('.amount');
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = '';

let totalAmount = 0;
const people = [];

for (let i = 0; i < names.length; i++) {
const name = names[i].value.trim();
const amount = parseFloat(amounts[i].value);
if (name && !isNaN(amount)) {
people.push({ name, amount });
totalAmount += amount;
}
}

const average = totalAmount / people.length;

const balance = people.map(person => ({
name: person.name,
balance: person.amount - average
}));

const owes = [];
const owed = [];

balance.forEach(person => {
if (person.balance < 0) {
owes.push(person);
} else if (person.balance > 0) {
owed.push(person);
}
});

while (owes.length && owed.length) {
const ower = owes[0];
const creditor = owed[0];

const amount = Math.min(-ower.balance, creditor.balance);

resultsDiv.innerHTML += `<p>${ower.name} owes ${creditor.name} Rs.${amount.toFixed(2)}</p><br>`;

ower.balance += amount;
creditor.balance -= amount;

if (ower.balance === 0) {
owes.shift();
}
if (creditor.balance === 0) {
owed.shift();
}
}
}
66 changes: 66 additions & 0 deletions Expense Splitter/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}

.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 500px;
}

h1 {
text-align: center;
margin-bottom: 20px;
}

.input-group {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}

.input-group input {
flex: 1;
margin-right: 10px;
padding: 5px;
}

.input-group button {
padding: 5px;
}

button {
width: 100%;
padding: 10px;
margin-top: 10px;
border: none;
background-color: #007bff;
color: white;
cursor: pointer;
border-radius: 4px;
}

button:hover {
background-color: #0056b3;
}

#results {
margin-top: 20px;
display: flex;
justify-content: center;
align-items: center;
font: 20px;
font-weight: bold;
}




Loading