Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- function makeEl(type, text, clazz) {
- let el = document.createElement(type);
- if (text !== null) {
- el.textContent = text;
- }
- if (clazz !== null) {
- el.setAttribute('class', clazz);
- }
- return el;
- }
- function makeBuyBtn(price, bookDiv) {
- let buyBtn = makeEl('button', `Buy it only for ${price} BGN`, null);
- buyBtn.addEventListener('click', function () {
- bookDiv.remove();
- profit += +price;
- document.getElementsByTagName('h1')[1].textContent = `Total Store Profit: ${profit} BGN`;
- });
- return buyBtn;
- }
- let btn = document.getElementsByTagName('button')[0];
- let oldBookShelf = document.getElementsByClassName('bookShelf')[0];
- let newBookShelf = document.getElementsByClassName('bookShelf')[1];
- let profit = 0;
- btn.addEventListener('click', function (e) {
- e.preventDefault();
- let [name, year, price] = Array.from(document.getElementsByTagName('input')).map(e => e.value);
- if (name === '' || year <= 0 || price <= 0) {
- console.log('Error');
- return;
- }
- let bookDiv = makeEl('div', null, 'book');
- bookDiv.appendChild(makeEl('p', `${name} [${year}]`, null));
- let priceNew = parseFloat(price).toFixed(2);
- let priceOld = (priceNew * 0.85).toFixed(2);
- if (+year < 2000) {
- bookDiv.appendChild(makeBuyBtn(priceOld, bookDiv));
- oldBookShelf.appendChild(bookDiv);
- } else {
- bookDiv.appendChild(makeBuyBtn(priceNew, bookDiv));
- let moveBtn = makeEl('button', `Move to old section`, null);
- moveBtn.addEventListener('click', function () {
- bookDiv.remove();
- bookDiv.lastChild.remove();
- bookDiv.lastChild.remove();
- bookDiv.appendChild(makeBuyBtn(priceOld, bookDiv));
- oldBookShelf.appendChild(bookDiv);
- });
- bookDiv.appendChild(moveBtn);
- newBookShelf.appendChild(bookDiv);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment