Advertisement
GeorgiLukanov87

Js Front-End - DOM and Events - Exercises

Feb 26th, 2023
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // DOM and Events - Exercises
  2. // https://judge.softuni.org/Contests/Compete/Index/3795#0
  3.  
  4.  
  5. // -----------------------------------------------------------------------------------------------------
  6. // 01. Subtraction
  7.  
  8. function subtract() {
  9.     let firstNumElement = document.getElementById('firstNumber')
  10.     let secondNumElement = document.getElementById('secondNumber')
  11.     let resultElement = document.getElementById('result')
  12.     let result = Number(firstNumElement.value) - Number(secondNumElement.value)
  13.     resultElement.textContent = result
  14. }
  15.  
  16. // -----------------------------------------------------------------------------------------------------
  17. // 02. Sections
  18.  
  19. function create(words) {
  20.    mainElement = document.getElementById('content')
  21.  
  22.    for (el of words) {
  23.       newDiv = document.createElement('div');
  24.       newP = document.createElement('p');
  25.       newP.textContent = el
  26.       newP.style.display = 'none'
  27.       newDiv.appendChild(newP)
  28.       mainElement.appendChild(newDiv)
  29.    }
  30.    
  31.    divElements = document.querySelectorAll('#content div')
  32.  
  33.    for (div of divElements) {
  34.       div.addEventListener('click', (e) => {
  35.          if (e.currentTarget.firstChild.style.display === 'none') {
  36.             console.log(e.currentTarget.firstChild.style.display = 'block')
  37.          } else {
  38.             e.currentTarget.firstChild.style.display = 'none'
  39.          }
  40.  
  41.       })
  42.    }
  43.  
  44. }
  45.  
  46. // -----------------------------------------------------------------------------------------------------
  47. // 03. Accordion
  48.  
  49. function toggle() {
  50.     btnElement = Array.from(document.getElementsByClassName('button'))[0]
  51.     textElement = document.getElementById('extra')
  52.    
  53.     if (btnElement.textContent === 'More') {
  54.         btnElement.textContent = 'Less'
  55.         textElement.style.display = 'block'
  56.     } else {
  57.         btnElement.textContent = 'More'
  58.         textElement.style.display = 'none'
  59.     }
  60.  
  61. }
  62.  
  63. // -----------------------------------------------------------------------------------------------------
  64. // 04. Locked Profile
  65.  
  66. function lockedProfile() {
  67.     let buttonsElements = Array.from(document.querySelectorAll('.profile button'))
  68.     buttonsElements.forEach(btn => btn.addEventListener('click', onClick))
  69.  
  70.     function onClick(event) {
  71.         let currentProfile = event.target.parentElement
  72.         let isLocked = currentProfile.querySelector('input[value="unlock"]').checked;
  73.         let toShow = currentProfile.querySelector(".profile div")
  74.         if (isLocked) {
  75.  
  76.             if (event.target.textContent === 'Show more') {
  77.                 toShow.style.display = 'block'
  78.                 event.target.textContent = 'Hide it'
  79.             }
  80.             else {
  81.                 toShow.style.display = 'none'
  82.                 event.target.textContent = 'Show more'
  83.             }
  84.         }
  85.  
  86.     }
  87. }
  88.  
  89. // -----------------------------------------------------------------------------------------------------
  90. // 05. Fill Dropdown
  91.  
  92. function addItem() {
  93.     dropMenuElement = document.getElementById('menu')
  94.     textElement = document.getElementById('newItemText')
  95.     valueElement = document.getElementById('newItemValue')
  96.  
  97.     newOptionElement = document.createElement('option')
  98.     newOptionElement.value = valueElement.value
  99.     newOptionElement.textContent = textElement.value
  100.      
  101.     dropMenuElement.appendChild(newOptionElement)
  102.  
  103.     textElement.value = ""
  104.     valueElement.value = ""
  105. }
  106.  
  107. // -----------------------------------------------------------------------------------------------------
  108. //06. Table – Search Engine
  109.  
  110. function solve() {
  111.    document.querySelector('#searchBtn').addEventListener('click', onClick);
  112.    let inputField = document.getElementById('searchField')
  113.    let rows = Array.from(document.querySelectorAll('tbody tr'))
  114.  
  115.    function onClick() {
  116.       console.log(inputField.value)
  117.       console.log(rows)
  118.       for (let row of rows) {
  119.          row.classList.remove('select');
  120.          if (inputField.value !== '' && row.innerHTML.includes(inputField.value)) {
  121.             row.className = 'select';
  122.          }
  123.       }
  124.       inputField.value = ''
  125.    }
  126. }
  127.  
  128. // -----------------------------------------------------------------------------------------------------
  129. // 07. Format the Text
  130.  
  131. function solve() {
  132.   let input = document.getElementById('input')
  133.   let output = document.getElementById('output')
  134.   let textArr = input.value.split('.').filter(s => s !== '');
  135.  
  136.   while (textArr.length > 0) {
  137.     let text = textArr.splice(0, 3).join('. ');
  138.     text += '.'
  139.     let newParagraph = document.createElement('p');
  140.     newParagraph.textContent = text;
  141.     output.appendChild(newParagraph);
  142.   }
  143.  
  144. }
  145.  
  146. // -----------------------------------------------------------------------------------------------------
  147. // 08. Furniture
  148.  
  149. function solve() {
  150.   let textarea = document.querySelectorAll('textarea');
  151.   let tbody = document.querySelector('tbody');
  152.  
  153.   [...document.querySelectorAll('button')].forEach(btn => btn.addEventListener('click', execute));
  154.   function execute(btn) {
  155.     if (!textarea[0].value) return;
  156.     if (btn.target.textContent === 'Generate') {
  157.       let input = JSON.parse(textarea[0].value);
  158.       input.forEach(furniture => {
  159.         tbody.innerHTML += `<tr>
  160.           <td><img src=${furniture.img}></td>
  161.           <td><p>${furniture.name}</p></td>
  162.           <td><p>${furniture.price}</p></td>
  163.           <td><p>${furniture.decFactor}</p></td>
  164.           <td><input type="checkbox"/></td>
  165.           </tr>`
  166.       })
  167.     } else {
  168.       let furnitureName = [];
  169.       let totalPrice = 0;
  170.       let averageDecFactor = 0;
  171.       [...document.querySelectorAll('input:checked')]
  172.         .forEach(furniture => {
  173.           let parentRow = furniture.parentNode.parentNode;
  174.           averageDecFactor += Number(parentRow.children[3].textContent);
  175.           totalPrice += Number(parentRow.children[2].textContent);
  176.           furnitureName.push(parentRow.children[1].textContent);
  177.         });
  178.       textarea[1].textContent += `Bought furniture: ${furnitureName.join(', ')}\n`;
  179.       textarea[1].textContent += `Total price: ${totalPrice.toFixed(2)}\n`;
  180.       textarea[1].textContent += `Average decoration factor: ${averageDecFactor / furnitureName.length}`;
  181.     }
  182.   }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement