-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (75 loc) · 1.85 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
const books = [
{
title: 'Book Title',
author: 'Author Name',
price: 19.99
},
{
title: 'Book Title',
author: 'Author Name',
price: 24.99
},
{
title: 'Book Title',
author: 'Author Name',
price: 14.99
}
];
const cart = [];
const addBookToCart = (book) => {
cart.push(book);
};
const removeBookFromCart = (book) => {
const index = cart.indexOf(book);
cart.splice(index, 1);
};
const calculateTotal = () => {
let total = 0;
cart.forEach((book) => {
total += book.price;
});
return total;
};
const renderBooks = () => {
const booksContainer = document.getElementById('books');
booksContainer.innerHTML = '';
books.forEach((book) => {
const bookElement = document.createElement('li');
bookElement.innerHTML = `
<div class="book-cover">
<img src="book-cover.jpg" alt="Book cover">
</div>
<div class="book-info">
<h3>${book.title}</h3>
<p>${book.author}</p>
<p>Price: $${book.price}</p>
<button>Add to Cart</button>
</div>
`;
booksContainer.appendChild(bookElement);
});
};
const renderCart = () => {
const cartContainer = document.getElementById('cart');
cartContainer.innerHTML = '';
cart.forEach((book) => {
const bookElement = document.createElement('li');
bookElement.innerHTML = `
<div class="book-cover">
<img src="book-cover.jpg" alt="Book cover">
</div>
<div class="book-info">
<h3>${book.title}</h3>
<p>${book.author}</p>
<p>Price: $${book. price}</p>
<button>Remove from Cart</button>
</div>
`;
cartContainer.appendChild(bookElement);
});
};
const main = () => {
renderBooks();
renderCart();
};
main();