Skip to content

Commit c388ddf

Browse files
committed
fix (splitBill): Added paid option
1 parent 110113d commit c388ddf

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

SplitBill/splitBill.html

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ <h1>Split bill</h1>
4545
<button id="redo" onclick="redo()">Redo</button>
4646
<button id="reset" onclick="reset()">Reset</button>
4747
<hr class="separator" />
48-
<input type="checkbox" id="hideCompleted" name="HideCompleted" onclick="fill()" checked/><label for="hideCompleted">Hide completed</label>
48+
<label><input type="checkbox" id="hideCompleted" onclick="fill()" checked/>Hide completed</label>
4949
</div>
5050
</div>
5151
<div>
@@ -274,6 +274,7 @@ <h3>Persons:</h3>
274274
id: id,
275275
name: `Person ${data.persons.length + 1}`,
276276
amounts: {},
277+
paid: false,
277278
});
278279
build();
279280
centerById(id);
@@ -339,17 +340,28 @@ <h3>Persons:</h3>
339340
fill();
340341
}
341342

343+
function personPaid(personId, paid) {
344+
data.persons.find(byId(personId)).paid = paid;
345+
fill();
346+
}
347+
342348
function centerById(id) {
343349
document.querySelector(`#id_${id}`).center();
344350
}
345351

346352
function gotoError() {
347-
data.items.forEach(item => {
353+
for (const item of data.items) {
348354
if (calculateItemPending(item) != 0) {
349355
centerById(item.id);
350356
return;
351357
}
352-
});
358+
}
359+
for (const person of data.persons) {
360+
if (!person.paid) {
361+
centerById(person.id);
362+
return;
363+
}
364+
}
353365
}
354366

355367
// generators
@@ -398,6 +410,7 @@ <h3>Persons:</h3>
398410
</div>`).join("\n")}
399411
<div class="flex">
400412
<p>Total: <span class="personTotal"></span></p>
413+
<label><input type="checkbox" class="personPaid" onclick="personPaid(${person.id}, this.checked)"/>Paid</label>
401414
</div>
402415
</div>`).join("\n");
403416

@@ -459,6 +472,8 @@ <h3>Persons:</h3>
459472
child.visible(!hideCompleted || itemPending != 0);
460473
});
461474
parent.querySelector(".personTotal").innerText = calculatePersonTotal(person).formatPrice();
475+
parent.querySelector(".personPaid").checked = person.paid;
476+
parent.style.backgroundColor = person.paid ? "lightgreen" : "lightpink";
462477
});
463478

464479
document.getElementById("itemsCount").innerText = data.items.length;
@@ -470,14 +485,15 @@ <h3>Persons:</h3>
470485
document.getElementById("personsPrice").innerText = personsPrice;
471486

472487
const totalDiff = (itemsPrice - personsPrice).formatPrice();
488+
const allPaid = data.persons.every(p => p.paid);
473489
document.getElementById("status").innerText =
474490
totalDiff > 0 ? `There is still a price of ${totalDiff} to assign` :
475491
totalDiff < 0 ? `There is an extra assigned price of ${-totalDiff}` :
476-
"The bill price matches";
492+
`The bill price matches ${allPaid ? 'and everyone payed' : 'but someone still needs to pay'}`;
477493
document.getElementById("status").style.backgroundColor =
478494
itemsPrice == 0 ? "lightgrey" :
479495
totalDiff > 0 ? "lightpink" :
480-
totalDiff < 0 ? "lightyellow" :
496+
totalDiff < 0 || !allPaid ? "lightyellow" :
481497
"lightgreen";
482498

483499
save();
@@ -515,8 +531,8 @@ <h3>Persons:</h3>
515531

516532
function sample(id) {
517533
data = JSON.parse({
518-
1: '{"persons":[{"id":1,"name":"Alice","amounts":{"4":1,"5":1,"7":0.1}},{"id":2,"name":"Bob","amounts":{"4":1,"6":1}},{"id":3,"name":"Charlie","amounts":{"4":1,"7":0.9}}],"items":[{"id":4,"name":"Water","price":2.2,"amount":3},{"id":5,"name":"Hamburger","price":14.95,"amount":1},{"id":6,"name":"Spaguetti","price":13.37,"amount":1},{"id":7,"name":"Pizza","price":15.25,"amount":1}]}',
519-
2: '{"persons":[{"id":14,"name":"Emma Crane","amounts":{"1":0.1,"6":0.1,"8":1,"13":1}},{"id":15,"name":"Macy Lucero","amounts":{"1":0.1,"4":1,"6":0.1,"8":1,"12":1,"13":0}},{"id":16,"name":"Iyla Odom","amounts":{"1":0.1,"6":0.1,"7":1}},{"id":17,"name":"Jake Lester","amounts":{"1":0.1,"6":0.1,"9":1,"12":1}},{"id":18,"name":"Castiel Parra","amounts":{"1":0.1,"6":0.1,"8":1,"11":1}},{"id":19,"name":"Karen Hart","amounts":{"1":0.1,"6":0.1,"7":1}},{"id":20,"name":"Wesley Orr","amounts":{"0":1,"1":0.1,"6":0.1,"10":1,"11":1}},{"id":21,"name":"Hudson Castillo","amounts":{"1":0.1,"6":0.1,"7":1,"12":1,"13":1}},{"id":22,"name":"Skyler Wilkinson","amounts":{"1":0.1,"6":0.1,"8":1,"11":1}},{"id":23,"name":"Tiffany Cortes","amounts":{"1":0.1,"6":0.1}}],"items":[{"id":4,"name":"Beer","price":3,"amount":1},{"id":0,"name":"soda","price":2.9,"amount":1},{"id":1,"name":"potatoes","price":7.9,"amount":1},{"id":6,"name":"salad","price":9.9,"amount":1},{"id":7,"name":"beef with cheese","price":13.9,"amount":3},{"id":8,"name":"chicken with bacon","price":14.5,"amount":5},{"id":9,"name":"chicken with curry","price":14.5,"amount":1},{"id":10,"name":"spicy pork","price":10.9,"amount":1},{"id":11,"name":"brownie","price":6.9,"amount":3},{"id":12,"name":"cheese cake","price":5.9,"amount":3},{"id":13,"name":"wine","price":2.9,"amount":2}]}',
534+
1: '{"persons":[{"id":1,"name":"Alice","amounts":{"4":1,"5":1,"7":0.1},"paid":true},{"id":2,"name":"Bob","amounts":{"4":1,"6":1},"paid":true},{"id":3,"name":"Charlie","amounts":{"4":1,"7":0.9},"paid":true}],"items":[{"id":4,"name":"Water","price":2.2,"amount":3},{"id":5,"name":"Hamburger","price":14.95,"amount":1},{"id":6,"name":"Spaguetti","price":13.37,"amount":1},{"id":7,"name":"Pizza","price":15.25,"amount":1}]}',
535+
2: '{"persons":[{"id":14,"name":"Emma Crane","amounts":{"1":0.1,"6":0.1,"8":1,"13":1},"paid":true},{"id":15,"name":"Macy Lucero","amounts":{"1":0.1,"4":1,"6":0.1,"8":1,"12":1,"13":0},"paid":true},{"id":16,"name":"Iyla Odom","amounts":{"1":0.1,"6":0.1,"7":1},"paid":true},{"id":17,"name":"Jake Lester","amounts":{"1":0.1,"6":0.1,"9":1,"12":1},"paid":true},{"id":18,"name":"Castiel Parra","amounts":{"1":0.1,"6":0.1,"8":1,"11":1},"paid":true},{"id":19,"name":"Karen Hart","amounts":{"1":0.1,"6":0.1,"7":1},"paid":true},{"id":20,"name":"Wesley Orr","amounts":{"0":1,"1":0.1,"6":0.1,"10":1,"11":1},"paid":true},{"id":21,"name":"Hudson Castillo","amounts":{"1":0.1,"6":0.1,"7":1,"12":1,"13":1},"paid":true},{"id":22,"name":"Skyler Wilkinson","amounts":{"1":0.1,"6":0.1,"8":1,"11":1},"paid":true},{"id":23,"name":"Tiffany Cortes","amounts":{"1":0.1,"6":0.1}}],"items":[{"id":4,"name":"Beer","price":3,"amount":1},{"id":0,"name":"soda","price":2.9,"amount":1},{"id":1,"name":"potatoes","price":7.9,"amount":1},{"id":6,"name":"salad","price":9.9,"amount":1},{"id":7,"name":"beef with cheese","price":13.9,"amount":3},{"id":8,"name":"chicken with bacon","price":14.5,"amount":5},{"id":9,"name":"chicken with curry","price":14.5,"amount":1},{"id":10,"name":"spicy pork","price":10.9,"amount":1},{"id":11,"name":"brownie","price":6.9,"amount":3},{"id":12,"name":"cheese cake","price":5.9,"amount":3},{"id":13,"name":"wine","price":2.9,"amount":2}]}',
520536
} [id]);
521537
build();
522538
}

0 commit comments

Comments
 (0)