script.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // таблица умножения
  2. let table = document.createElement('table')
  3. table.id = 'mulTable'
  4. for(let i = 1; i < 10; i++) {
  5. let tr = document.createElement('tr')
  6. for(let j = 1; j < 10; j++) {
  7. let td = document.createElement('td')
  8. td.style.textAlign = 'center'
  9. td.style.padding = '5px'
  10. td.style.border = '1px solid dimgrey'
  11. td.innerText = `${i * j}`
  12. tr.appendChild(td)
  13. }
  14. table.appendChild(tr)
  15. }
  16. document.getElementsByTagName('body')[0].appendChild(table)
  17. // подсветить ячейку и строку и колонку
  18. document.getElementById('mulTable').addEventListener('mouseover', (e)=> {
  19. if(e.target.closest('td')) {
  20. for(let i of e.currentTarget.children) {
  21. i.children[e.target.cellIndex].style.backgroundColor = 'black'
  22. i.children[e.target.cellIndex].style.color = 'white'
  23. }
  24. e.target.closest('td').style.backgroundColor = 'yellow'
  25. e.target.closest('td').style.color = 'black'
  26. e.target.closest('tr').style.backgroundColor = 'magenta'
  27. }
  28. })
  29. document.getElementById('mulTable').addEventListener('mouseout', (e)=> {
  30. if(e.target.closest('td')) {
  31. for(let i of e.currentTarget.children) {
  32. for(let j of i.children) {
  33. j.style.backgroundColor = 'transparent'
  34. j.style.color = 'black'
  35. }
  36. }
  37. e.target.closest('td').style.backgroundColor = 'transparent'
  38. e.target.closest('tr').style.backgroundColor = 'transparent'
  39. }
  40. })
  41. //calc
  42. let inp1 = document.createElement('input')
  43. let inp2 = document.createElement('input')
  44. let res = document.createElement('input')
  45. res.disabled = true
  46. inp1.type = 'number'
  47. inp2.type = 'number'
  48. res.type = 'number'
  49. let title = document.createElement('h2')
  50. title.innerText = 'calc'
  51. let plus = document.createElement('span')
  52. plus.innerText= ' + '
  53. let equ = document.createElement('span')
  54. equ.innerText = ' = '
  55. let btn = document.createElement('button')
  56. btn.id = 'countBtn'
  57. btn.innerText = 'Count'
  58. document.body.appendChild(title)
  59. document.body.appendChild(inp1)
  60. document.body.appendChild(plus)
  61. document.body.appendChild(inp2)
  62. document.body.appendChild(equ)
  63. document.body.appendChild(res)
  64. document.body.appendChild(btn)
  65. btn.onclick = () => {
  66. inp1.value || inp2.value? res.disabled = false : res.disabled = true
  67. res.value = (+inp1.value) + (+inp2.value)
  68. }
  69. //сalc live
  70. document.body.insertAdjacentHTML('beforeend', `
  71. <h3>calc live</h3>
  72. <div>
  73. <div>
  74. <select id="operation">
  75. <option value="sell">SELL</option>
  76. <option value="buy">BUY</option>
  77. </select>
  78. <input type="number" id="given">
  79. <select id="currency">
  80. <option value="eur">EUR</option>
  81. <option value="usd">USD</option>
  82. </select>
  83. </div>
  84. <br>
  85. <label><span>You get: </span><input type="number" id="result"> HRN</label>
  86. </div>
  87. `)
  88. let given = document.getElementById('given')
  89. let operation = document.getElementById('operation')
  90. let currency = document.getElementById('currency')
  91. document.getElementById('result').disabled = true
  92. let ratios = {
  93. sell: {
  94. usd: 0.038,
  95. eur: 0.033
  96. },
  97. buy: {
  98. usd: 0.037,
  99. eur: 0.032,
  100. }
  101. }
  102. given.addEventListener('input', calculation)
  103. operation.addEventListener('change', calculation)
  104. currency.addEventListener('change', calculation)
  105. function truncateFraction (number) {
  106. try {
  107. let strNum = String(number).split('.')
  108. if(strNum[1].length > 2) {
  109. strNum[1] = strNum[1].slice(0, 2)
  110. }
  111. return Number(strNum.join('.'))
  112. } catch (e) {
  113. return number
  114. }
  115. }
  116. function calculation() {
  117. operation.value === 'buy'? document.querySelector('label').children[0].innerText = 'You give: ':
  118. document.querySelector('label').children[0].innerText = 'You get: '
  119. if(+given.value <= 0 || given.value === '') {
  120. document.getElementById('result').value = ''
  121. document.getElementById('result').disabled = true
  122. } else {
  123. document.getElementById('result').disabled = false
  124. document.getElementById('result').value = truncateFraction(given.value / ratios[operation.value][currency.value])
  125. }
  126. }