Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. const inputs = {
  2. elemento_numeroCarreiras: document.querySelector('#numeroCarreiras'),
  3. elemento_numeroArvores: document.querySelector('#numeroArvores'),
  4. elemento_diametroBase: document.querySelector('#diametroBase'),
  5. elemento_diametroCopa: document.querySelector('#diametroCopa'),
  6. elemento_compMadeira: document.querySelector('#compMadeira'),
  7. };
  8.  
  9. /** Input Validation */
  10.  
  11. const memory = {};
  12.  
  13. for (const key of Object.keys(inputs)) {
  14. const input = inputs[key];
  15.  
  16. memory[input.id] = Number(input.value);
  17. }
  18.  
  19. for (const key of Object.keys(inputs)) {
  20. const input = inputs[key];
  21.  
  22. let isShiftBeingPressed = false;
  23.  
  24. input.onkeydown = event => {
  25. const keyCode = event.keyCode;
  26.  
  27. let allow = false;
  28.  
  29. // Allow numbers
  30. if (keyCode >= 48 && keyCode <= 57) allow = true;
  31. // Allow backspace
  32. if (keyCode === 8) allow = true;
  33. // Allow ,
  34. if (keyCode === 188) allow = true;
  35. // Allow .
  36. if (keyCode === 190) allow = true;
  37. // Allow -
  38. // if (keyCode === 189) allow = true;
  39.  
  40. // Allow keyboard arrows (navigation)
  41. if (keyCode >= 37 && keyCode <= 40) allow = true;
  42.  
  43. // Allow tab
  44. if (keyCode === 9) allow = true;
  45.  
  46. if (keyCode === 16) isShiftBeingPressed = true;
  47.  
  48. if (!allow) event.preventDefault();
  49. };
  50.  
  51. input.onkeyup = event => {
  52. const keyCode = event.keyCode;
  53.  
  54. if (keyCode === 16) isShiftBeingPressed = false;
  55. };
  56.  
  57. const regexMoreThanOneDot = /[.].*[.]/; // Regex que verifica se existem mais que um .
  58. const regexMoreThanOneMinus = /[-].*[-]/; // Regex que verifica se existem mais que um -
  59. // const regexContainMinus = /[-]{1,}/; // Regex que verifica se existem -
  60. // const regexBeginsWithMinus = /^-/; // Regex que verifica se começa com -
  61.  
  62. input.oninput = event => {
  63. event.target.value = event.target.value.replace(/,/g, '.'); // Troca , por .
  64.  
  65. let allow = true;
  66.  
  67. if (regexMoreThanOneDot.test(event.target.value)) allow = false;
  68. if (regexMoreThanOneMinus.test(event.target.value)) allow = false;
  69. // if (
  70. // regexContainMinus.test(event.target.value) &&
  71. // !regexBeginsWithMinus.test(event.target.value)
  72. // )
  73. // allow = false;
  74.  
  75. if (isShiftBeingPressed) allow = false;
  76.  
  77. if (allow) memory[input.id] = event.target.value;
  78. else event.target.value = memory[input.id];
  79. };
  80. }
  81.  
  82. /** Input methods */
  83.  
  84. function limpar() {
  85. for (const key of Object.keys(inputs)) {
  86. const input = inputs[key];
  87.  
  88. input.value = 0;
  89. memory[input.id] = 0;
  90. }
  91.  
  92. // Reset result
  93. document.querySelector('#result').innerHTML = '----';
  94. }
  95.  
  96. function calcular() {
  97. const numeroCarreiras = Number(inputs.elemento_numeroCarreiras.value);
  98. const numeroArvores = Number(inputs.elemento_numeroArvores.value);
  99. const diametroBase = Number(inputs.elemento_diametroBase.value);
  100. const diametroCopa = Number(inputs.elemento_diametroCopa.value);
  101. const compMadeira = Number(inputs.elemento_compMadeira.value);
  102.  
  103. const mediaDiametro = (diametroBase + diametroCopa) / 2;
  104. const raio = mediaDiametro / 2;
  105. const cxA = numeroCarreiras * numeroArvores;
  106.  
  107. const calculoInicial = Math.PI * Math.pow(raio / 100, 2) * compMadeira;
  108. const calculoFinal = calculoInicial * cxA;
  109.  
  110. // Shows result on page
  111. document.querySelector('#result').innerHTML = `${calculoFinal.toFixed(1)} m²`;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement