Skip to content

Commit d8a4e75

Browse files
authored
splitBill: new normalize item button
1 parent 9d181dd commit d8a4e75

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

SplitBill/splitBill.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,17 @@ <h3>Persons:</h3>
348348
fill();
349349
}
350350

351+
function normalizeItem(itemId){
352+
const item = data.items.find(byId(itemId));
353+
354+
const sum = data.persons.map(person => getAmount(person, item)).sum();
355+
if(sum === 0){
356+
return;
357+
}
358+
data.persons.filter(person => itemId in person.amounts).forEach(person => person.amounts[itemId] *= item.amount / sum);
359+
fill();
360+
}
361+
351362
function personPaid(personId, paid) {
352363
data.persons.find(byId(personId)).paid = paid;
353364
fill();
@@ -411,7 +422,8 @@ <h3>Persons:</h3>
411422
</div>`).join("\n")}
412423
<div class="flex">
413424
<button onclick="splitEqually(${item.id})">Split equally</button>
414-
<button onclick="clearItem(${item.id})">Clear item</button>
425+
<button onclick="clearItem(${item.id})">Clear</button>
426+
<button onclick="normalizeItem(${item.id})">Normalize</button>
415427
<p class="itemPending"></p>
416428
</div>
417429
</div>`).join("\n");

0 commit comments

Comments
 (0)