-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
152 lines (127 loc) · 5.07 KB
/
script.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// initialize the library to an empty array
const myLibrary = [];
// create a constructor function to create new book instances
function Book(title, author, pages, read) {
this.Title = title;
this.Author = author;
this.Pages = pages;
this.Read = read;
}
// add a prototype function to toggle the book read status
Book.prototype.readToggle = function(input) {
if(input){
this.Read = 'no';
}else{
this.Read = 'yes';
}
}
// create a function that creates a new book from the users input and pushes it to the library
function addBookToLibrary(title, author, pages, read) {
const book = new Book(title, author, pages, read);
myLibrary.push(book)
console.log(myLibrary);
}
// i created template books to visualize the table
const book1 = new Book('The Lord Of The Rings', 'GGR Martins', 478, 'yes');
const book2 = new Book('Fifty Shades Of Grey', 'Camila Ceballo', 350, 'no');
const book3 = new Book('Harry Potter and the sorcerer\'s stone', 'JK Rowling', 597, 'yes');
const book4 = new Book('A Tale Of Fire And Ice', 'Martin Scorsecee', 862, 'yes');
myLibrary.push(book1, book2, book3, book4);
// create the table header using javascript and append it to the html table div
const books = myLibrary[0];
const tableWrapper = document.querySelector('.table-wrapper')
const table = document.createElement('table');
const headRow = document.createElement('tr');
const headRowData = Object.keys(books)
headRowData.forEach(data => {
const tableHead = document.createElement('th');
tableHead.append(data)
headRow.appendChild(tableHead)
table.appendChild(headRow)
})
tableWrapper.appendChild(table);
// create a function that selects the last item on the libray and append it to the end of the table
function displayNewBook(){
const book = myLibrary.slice(-1)[0]
console.log(book);
const tableRow = document.createElement('tr');
const titleData = document.createElement('td');
const authorData = document.createElement('td');
const pagesData = document.createElement('td');
const readStatus = document.createElement('td');
tableRow.style = "border-bottom: 1px solid #000;"
titleData.textContent = book.Title;
authorData.textContent = book.Author;
pagesData.textContent = book.Pages;
readStatus.textContent = book.Read;
readStatus.style = 'text-transform: uppercase;'
// create a read button to toggle the read status of the current book
const readButton = document.createElement('button');
readButton.classList.add('toggle')
readButton.textContent = 'completed'
readButton.addEventListener('click', () => {
if(book.Read === 'yes'){
book.readToggle(true)
readStatus.textContent = 'no'
} else{
book.readToggle(false)
readStatus.textContent = 'yes'
}
})
// create a remove button to remove the current book
const removeButton = document.createElement('button');
removeButton.textContent = 'remove book'
removeButton.addEventListener('click', () =>{
table.removeChild(tableRow)
})
tableRow.appendChild(titleData);
tableRow.appendChild(authorData);
tableRow.appendChild(pagesData);
tableRow.appendChild(readStatus);
tableRow.appendChild(readButton);
tableRow.appendChild(removeButton);
table.appendChild(tableRow)
}
// grab all the form elements from the DOM
const titleInput= document.getElementById('title');
const authorInput= document.getElementById('author');
const pagesInput= document.getElementById('pages');
const readEls = document.querySelectorAll('.read-status');
const submitBtn = document.getElementById('submit');
// create an event listener that is triggered whenever the submit button is clicked
submitBtn.addEventListener('click', (e) =>{
const inputFields = [...document.querySelectorAll('input')];
// check if all the input fields have been filled
const allValid = inputFields.every(input => input.reportValidity())
if (allValid){
e.preventDefault();
// create a function that capitalizes the first letter of each word of the user input
function captitalize(input) {
return input.split(' ').map(word => word.charAt(0).toUpperCase() + word.substr(1)).join(' ')
}
const bookTitle = captitalize(titleInput.value);
const authorName = captitalize(authorInput.value);
const pages = pagesInput.value;
// create a check for the pages input to confirm if its a valid number
if(isNaN(pages) || pages < 1){
alert('please enter a valid page number')
return false
}
//this loops the radio buttons and stores its value into a variable and then clears it when the page reloads
let readInfo = undefined
readEls.forEach(option => {
if (option.checked){
readInfo = option.value
option.checked = false
}
})
// take all user input and call the function that turns it into a book object and adds it to the array
addBookToLibrary(bookTitle, authorName, pages, readInfo);
//the new book becomes the last item in the array and this function displays it
displayNewBook()
// clear all inputs after submitting
titleInput.value = ''
authorInput.value = ''
pagesInput.value = ''
} else return
})