script.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const ratios = {
  2. pnd : {
  3. multiplier: 0.45359,
  4. transition: 'kg'
  5. },
  6. kg : {
  7. multiplier: 2.20462,
  8. transition: 'pnd'
  9. },
  10. mil: {
  11. multiplier: 1.60934,
  12. transition: 'km'
  13. },
  14. km : {
  15. multiplier: 0.62137,
  16. transition: 'mil'
  17. },
  18. inch : {
  19. multiplier: 2.54,
  20. transition: 'cm'
  21. },
  22. cm : {
  23. multiplier: 0.3937,
  24. transition: 'inch'
  25. }
  26. }
  27. const texts = [
  28. {
  29. title: 'Pound(lb.)',
  30. text: `The pound or pound-mass is a unit of mass used in British imperial and United States customary systems of measurement. Various definitions have been used; the most common today is the international avoirdupois pound, which is legally defined as exactly 0.45359237 kilograms, and which is divided into 16 avoirdupois ounces. The international standard symbol for the avoirdupois pound is lb; an alternative symbol is lbm (for most pound definitions), # (chiefly in the U.S.), and ℔ or ″̶ (specifically for the apothecaries' pound).
  31. The unit is descended from the Roman libra (hence the abbreviation "lb"). The English word pound is cognate with, among others, German Pfund, Dutch pond, and Swedish pund. These units are historic and are no longer used formally (replaced by the metric system), though informal use of the term to mean 500 grams is still heard across Europe and beyond.
  32. Usage of the unqualified term pound reflects the historical conflation of mass and weight. This accounts for the modern distinguishing terms pound-mass and pound-force.'`,
  33. tip: `to get approximate result multiply 'mass' value to 2,205`
  34. },
  35. {
  36. title: 'Mile(mi.)',
  37. text: `The mile, sometimes the international mile or statute mile to distinguish it from other miles, is a British imperial unit and US customary unit of distance; both are based on the older English unit of length equal to 5,280 English feet, or 1,760 yards. The statute mile was standardised between the British Commonwealth and the United States by an international agreement in 1959, when it was formally redefined with respect to SI units as exactly 1,609.344 metres.
  38. With qualifiers, mile is also used to describe or translate a wide range of units derived from or roughly equivalent to the Roman mile, such as the nautical mile (now 1.852 km exactly), the Italian mile (roughly 1.852 km), and the Chinese mile (now 500 m exactly). The Romans divided their mile into 5,000 Roman feet but the greater importance of furlongs in Elizabethan-era England meant that the statute mile was made equivalent to 8 furlongs or 5,280 feet in 1593. This form of the mile then spread across the British Empire, some of which continues to employ the mile. The US Geological Survey now employs the metre for official purposes, but legacy data from its 1927 geodetic datum has meant that a separate US survey mile (
  39. 6336/3937km) continues to see some use, although it will be officially phased out in 2022. While most countries replaced the mile with the kilometre when switching to the International System of Units (SI), the international mile continues to be used in some countries, such as Liberia, the United Kingdom, the United States, and a number of countries with fewer than one million inhabitants, most of which are UK or US territories or have close historical ties with the UK or US.`,
  40. tip: `to get approximate result multiply 'width' value to 1,609`
  41. },
  42. {
  43. title: 'Inch(")',
  44. text: `The inch (symbol: in or ″) is a unit of length in the British imperial and the United States customary systems of measurement. It is equal to 1/36 yard or 1/12
  45. of a foot. Derived from the Roman uncia ("twelfth"), the word inch is also sometimes used to translate similar units in other measurement systems, usually understood as deriving from the width of the human thumb.
  46. Standards for the exact length of an inch have varied in the past, but since the adoption of the international yard during the 1950s and 1960s it has been based on the metric system and defined as exactly 25.4 mm.`,
  47. tip: `to get approximate result multiply 'length' value to 2,54`
  48. }
  49. ]
  50. const container = document.querySelector('.container')
  51. const reader = document.querySelector('.reader__wrapper')
  52. let currentSlide = 0
  53. function truncateFraction (number) {
  54. try {
  55. let strNum = String(number).split('.')
  56. if(strNum[1].length > 3) {
  57. strNum[1] = strNum[1].slice(0, 3)
  58. }
  59. return Number(strNum.join('.'))
  60. } catch (e) {
  61. return number
  62. }
  63. }
  64. function isValidInput (value) {
  65. if(value < 0 || value > 999999999999999999999) {
  66. return true
  67. } else {
  68. return false
  69. }
  70. }
  71. function invokeError(bool, element, dependent) {
  72. try {
  73. if(bool) {
  74. if(element.getElementsByTagName('small')[0]) {
  75. element.getElementsByTagName('small')[0].remove()
  76. }
  77. dependent.setAttribute('disabled', true)
  78. setBackgroundColor('gold', dependent.closest('div'))
  79. setBackgroundColor('red', element.closest('div'))
  80. element.insertAdjacentHTML('beforeend', `
  81. <small>Invalid value given</small>
  82. `)
  83. } else {
  84. dependent.removeAttribute('disabled')
  85. setBackgroundColor('transparent', dependent.closest('div'), element.closest('div'))
  86. element.getElementsByTagName('small')[0].remove()
  87. element.style.backgoundColor = transparent;
  88. }
  89. } catch(e) {
  90. return;
  91. }
  92. }
  93. function setBackgroundColor (color, ...elements) {
  94. elements.forEach(element => element.setAttribute('style', `background-color: ${color}`))
  95. }
  96. container.addEventListener('input', (e) => {
  97. invokeError(isValidInput(e.target.value), e.target.closest('div'), document.getElementById(ratios[`${e.target.id}`].transition))
  98. document.getElementById(ratios[`${e.target.id}`].transition).value = truncateFraction(e.target.value * ratios[`${e.target.id}`].multiplier)
  99. })
  100. reader.addEventListener('click', (e) => {
  101. switch(e.target.id) {
  102. case 'goleft':
  103. if(currentSlide <= 0) {
  104. currentSlide = texts.length - 1
  105. } else {
  106. currentSlide--
  107. }
  108. break;
  109. case 'goright':
  110. if(currentSlide >= texts.length - 1) {
  111. currentSlide = 0
  112. } else {
  113. currentSlide++
  114. }
  115. break;
  116. }
  117. reader.getElementsByTagName('h2')[0].innerHTML = texts[currentSlide].title
  118. reader.getElementsByTagName('p')[0].innerHTML = texts[currentSlide].text
  119. reader.getElementsByTagName('p')[1].remove()
  120. reader.getElementsByTagName('article')[0].insertAdjacentHTML('beforeend', `
  121. <p><strong>Tip:</strong> ${texts[currentSlide].tip}</p>
  122. `)
  123. setBackgroundColor('gold', reader.getElementsByTagName('strong')[0])
  124. reader.getElementsByTagName('strong')[0].style.color = 'black';
  125. })