-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
62 lines (32 loc) · 1.41 KB
/
javascript.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
//Get myLibrary array
var myLibrary = [];
const myButton = document.getElementById("add");
//Create a book
function Book(title, author, numPages){
this.title = title;
this.author = author;
this.numPages = numPages;
}
//Add the book to the array
function addBookToLibrary(){
//Put variables for names
let title = document.forms["addBook"]["name"].value;
let author = document.forms["addBook"]["author"].value;
let numPages = document.forms["addBook"]["page_number"].value;
let myBook = new Book(title, author, numPages);
// ✅ only runs if value not in array
myLibrary.push(myBook);
}
//Create and show all the books
function displayBooks(){
for (let i = 0; i < myLibrary.length; i++){
let bookCover = document.createElement("div");
bookCover.textContent = myLibrary[i].title + " by " + myLibrary[i].author + " with " + myLibrary[i].pageNum + " pages";
bookCover.classList.toggle('book_cover');
document.getElementById("bookShelf").appendChild(bookCover);
}
}
myButton.addEventListener("click", function(event) {
addBookToLibrary();
displayBooks();
});