svetlyoek

Untitled

Jun 26th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. function solve() {
  2.  
  3. const title = document.querySelectorAll('form input')[0];
  4. const year = document.querySelectorAll('form input')[1];
  5. const price = document.querySelectorAll('form input')[2];
  6. const addBtn = document.querySelector('form button');
  7.  
  8. const totalProfit = document.querySelectorAll('h1')[1];
  9. const oldBooksShelf = document.querySelector('body #outputs section:nth-child(1) .bookShelf');
  10. const newBooksShelf = document.querySelector('body #outputs section:nth-child(2) .bookShelf');
  11.  
  12. let totalSum = 0;
  13.  
  14. addBtn.addEventListener('click', function (e) {
  15. e.preventDefault();
  16.  
  17. titleValue = title.value.trim();
  18.  
  19. /* Parse */
  20.  
  21. yearValue = Number(year.value);
  22. priceValue = Number(price.value);
  23.  
  24. if (titleValue !== '' && yearValue >= 0 && priceValue >= 0) {
  25.  
  26. const div = document.createElement('div');
  27. div.classList.add('book');
  28.  
  29. const paragraph = document.createElement('p');
  30. const buyItBtn = document.createElement('button');
  31. const moveBtn = document.createElement('button');
  32.  
  33. paragraph.textContent = `${titleValue} [${yearValue}]`;
  34. div.appendChild(paragraph);
  35.  
  36. if (yearValue >= 2000) {
  37.  
  38. buyItBtn.textContent = `Buy it only for ${priceValue.toFixed(2)} BGN`;
  39. moveBtn.textContent = `Move to old section`;
  40. div.appendChild(buyItBtn);
  41. div.appendChild(moveBtn);
  42.  
  43. newBooksShelf.appendChild(div);
  44.  
  45. } else {
  46.  
  47. priceValue -= priceValue * 0.15;
  48. buyItBtn.textContent = `Buy it only for ${priceValue.toFixed(2)} BGN`;
  49. div.appendChild(buyItBtn);
  50.  
  51. oldBooksShelf.appendChild(div);
  52. }
  53.  
  54. moveBtn.addEventListener('click', function (e) {
  55.  
  56. priceValue -= priceValue * 0.15;
  57. buyItBtn.textContent = `Buy it only for ${priceValue.toFixed(2)} BGN`;
  58. moveBtn.remove();
  59. oldBooksShelf.appendChild(div);
  60. });
  61.  
  62. buyItBtn.addEventListener('click', function (e) {
  63.  
  64. div.remove();
  65. totalSum += priceValue;
  66.  
  67. totalProfit.textContent = `Total Store Profit: ${totalSum.toFixed(2)} BGN`;
  68.  
  69. });
  70. }
  71. });
  72. }
Add Comment
Please, Sign In to add comment