Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let oldBooks = document.querySelectorAll('#outputs .bookShelf')[0];
- let newBooks = document.querySelectorAll('#outputs .bookShelf')[1];
- document.querySelector('body form button').addEventListener('click', addBook);
- function addBook(e) {
- e.preventDefault();
- const fields = document.querySelectorAll('body form input');
- const title = fields[0].value;
- const year = Number(fields[1].value);
- let price = Number(fields[2].value);
- //triple condition
- if(title === '' || year < 1 || price <= 0) { return; }
- const book = createBook(title,year,price);
- year >= 2000 ? newBooks.appendChild(book) : oldBooks.appendChild(book);
- }
- function createBook(title, year, price) {
- let divBook = document.createElement('div');
- divBook.className = 'book';
- let paragraphBook = document.createElement('p');
- paragraphBook.textContent = `${title} [${year}]`;
- divBook.appendChild(paragraphBook);
- year < 2000 ? price = discount(price) : price;
- let buttonBuyBook = addButton(`Buy it only for ${price.toFixed(2)} BGN`);
- buttonBuyBook.addEventListener('click', buyBook);
- divBook.appendChild(buttonBuyBook);
- if(year >= 2000) {
- let buttonOldSection = addButton('Move to old section');
- buttonOldSection.addEventListener('click', toOldSection);
- divBook.appendChild(buttonOldSection);
- }
- return divBook;
- }
- function discount(price) {
- return price * 0.85;
- }
- function addButton(context) {
- let button = document.createElement('button');
- button.textContent = context;
- return button;
- }
- function buyBook(e) {
- let storeProfit = document.querySelectorAll('body h1')[1];
- let oldProfit = Number(storeProfit.textContent.split(' ')[3]);
- let bookPrice = Number(this.textContent.split(' ')[4]);
- storeProfit.textContent = `Total Store Profit: ${(oldProfit + bookPrice).toFixed(2)} BGN`;
- this.parentNode.remove();
- }
- function toOldSection(e) {
- let bookMoved = this.parentNode;
- //remove only button to old sectio!!!
- this.remove();
- let buyBook = bookMoved.querySelector('button');
- let oldPrice = Number(buyBook.textContent.split(' ')[4]);
- buyBook.textContent = `Buy it only for ${discount(oldPrice).toFixed(2)} BGN`;
- oldBooks.appendChild(bookMoved);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement