Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Laurence kite
11,768 PointsPracticing Objects video is confusing
I am confused by the code below, I have provided my answer to the chargeFines method which works and the teachers but I can not see where she updates the patrons array.
She never directly updates it but when she logs the patron object to console it has updated. ?
class Library {
constructor() {
this.books = [];
this.patrons = [];
this.dailyFine = 0.1;
}
addBook(book) {
// addd books to the books array
this.books.push(book);
}
addPatron(patron) {
// add patrons to the patrons array
this.patrons.push(patron);
}
chargeFines() {
//fines are charged for overdue books by 1 cent per day over duedate
// use filter to return a list of patrons who's books are over due..
// get todays date
var daysOverDue = [];
const daysAllowed = 14;
const today = new Date();
const overDue = this.patrons.filter((patron) =>{
if (patron.currentBook != null) {
var dueDate = patron.currentBook.dueDate;
var timeDiff = Math.abs(dueDate.getTime() - today.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
}
if (diffDays > 14) {
return patron;
}
})
//console.log(overDue );
for(let i = 0; i < overDue.length; i++) {
let dueDate = patron.currentBook.dueDate;
let timeBetween = Math.abs(dueDate.getTime() - today.getTime());
let totalDaysOut = Math.floor(timeBetween / (1000 * 3600 * 24));
let fine = (totalDaysOut * this.dailyFine);
if (this.patrons[i].name === overDue[i].name) {
this.patrons[i].balance = fine;
}
}
}
}
/* Her solution is more elegant but doesnt seem to update the patrons array held in this class (Library) ?
chargeFines() {
const now = new Date();
const latePatrons = this.patrons.filter(patron =>
(patron.currentBook !== null && patron.currentBook.dueDate < now)
);
for (let patron of latePatrons) {
const dateDiff = new Date(now - patron.currentBook.dueDate);
const daysLate = dateDiff.getDate();
patron.balance += this.dailyFine * daysLate;
}
}
The return is implicit
*/
In her code in the comments above she returns an array into an array called latePatrons via using the filter method on this.patrons.filter which will not update the patrons array but return a new array to latePatrons.
She then iterates over this array via the for loop and updates the balance property.
In index. html
she uses console.log(patron) which is the name of the object an it reflects the change?
<script>
// our test code here
const library = new Library();
const book = new Book('Harry Potter and the Sorcerer\'s Stone', 'J.K. Rowling', '978-0439708180');
const bookB = new Book('Art of fighting', 'Bruce Leee ', '556677');
const patron = new Patron("Laurence Kite", "lawwz@hotmail.com");
const patron2 = new Patron("Bruce Lee", "Lee@kungfu.com");
library.addPatron(patron);
library.addPatron(patron2);
library.addBook(book);
library.addBook(bookB);
patron.checkOut(book);
patron2.checkOut(bookB);
console.log(patron);
console.log(patron2);
library.chargeFines();
console.log(patron);
console.log(patron2);
</script>
She has not updated the object directly, but a returned copy. ?