-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (77 loc) · 2.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
class BookList {
constructor () {
this._numberOfBooksRead = 0
this._numberOfUnreadBooks = 0
this._currentBook = null
this._nextBook = null
this._previousBook = null
this._books = []
}
add (book) {
this._books.push(book)
}
startReadingFirstBook () {
// check to ensure array has books inside
// if it does, then set the currentBook to the first "unread" book in the _books array
if (this._books.length > 0) {
this._currentBook = this._books.find((book) => book.read === false)
}
}
finishCurrentBook () {
// mark the current book that is being read as "read = true"
this._currentBook.markAsRead()
// Give it a read date of new Date()
this._currentBook.dateRead = new Date()
// Change the last **book** read to be the book that just got finished
this._previousBook = this._currentBook
// set the new _currentBook to the next book in the _books array
// find the first book in the array that has not been read
this._currentBook = this._books.find((book) => book.read === false)
// Change the next **book** to be read property to be the first unread book you find in the list of books
this._nextBook = this._books.filter((book) => book.read === false)[1]
// this._nextBook = this._books[indexOfCurrentBook + 2]
}
}
class Book {
constructor (title, genre, author, read) {
this._title = title
this._genre = genre
this._author = author
this._read = read // boolean
this._dateRead = null
}
get read () {
return this._read
}
markAsRead () {
this._read = true
}
set dateRead (newDate) {
this._dateRead = newDate
}
}
const odessey = new Book('The Odessey', 'epic poem', 'Homer', false)
const huckFinn = new Book('Huckberry Finn', 'fiction', 'Tom Sawyer', false)
const winningFriends = new Book('how to win friends and influence people', 'non-fiction', 'Dale Carnegie ', true)
const rasinInSun = new Book('Raisin in Sun', 'fiction', 'Lorraine Hansberry', false)
const leanStartup = new Book('Lean Startup', 'non-fiction', 'Eric Ries', true)
const tenXRule = new Book('The 10X Rule', 'non-fiction', 'Grant Cardone', false)
const bookList = new BookList()
// console.log(odessey)
// console.log(huckFinn)
console.log(bookList)
bookList.add(odessey)
bookList.add(huckFinn)
bookList.add(winningFriends) // read = true
bookList.add(raisinInSun)
bookList.add(leanStartup) // read = true
bookList.add(tenXRule)
console.log(bookList)
bookList.startReadingFirstBook()
console.log(bookList)
bookList.finishCurrentBook()
console.log(bookList)
bookList.finishCurrentBook()
console.log(bookList)
bookList.finishCurrentBook()
console.log(bookList)