Advertisement
Guest User

modern js

a guest
Dec 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  4. <style>
  5. .tlo{
  6. position: relative;
  7. border: 2px solid black;
  8. width: 200px;
  9. height: 228px;
  10. border-radius: 10px;
  11. }
  12.  
  13. .napis1{
  14. position: absolute;
  15. left: 10px;
  16. top: 20px;
  17. }
  18. .napis2{
  19. position: absolute;
  20. left: 10px;
  21. top: 50px;
  22. }
  23. .napis3{
  24. position: absolute;
  25. left: 10px;
  26. top: 80px;
  27. }
  28.  
  29. .pole1{
  30. position: absolute;
  31. left: 100px;
  32. top: 18px;
  33. border: 1px solid black;
  34. }
  35. .pole2{
  36. position: absolute;
  37. left: 100px;
  38. top: 48px;
  39. border: 1px solid black;
  40. }
  41. .pole3{
  42. position: absolute;
  43. left: 100px;
  44. top: 78px;
  45. border: 1px solid black;
  46. }
  47.  
  48. .licz{
  49. position: absolute;
  50. left: 80px;
  51. top: 110px;
  52. border: 1px solid black;
  53. }
  54.  
  55. .crownanie{
  56. position: absolute;
  57. left: 50px;
  58. top: 130px;
  59. color: red;
  60. }
  61. .cx1{
  62. position: absolute;
  63. left: 20px;
  64. top: 165px;
  65. color: red;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70.  
  71. <!-- formularz -->
  72. <div class="tlo">
  73. <form name="formularz">
  74.  
  75. <!-- parametry -->
  76. <div class="napis1">
  77. Parametr a:
  78. </div>
  79.  
  80. <div class="napis2">
  81. Parametr b:
  82. </div>
  83.  
  84. <div class="napis3">
  85. Parametr c:
  86. </div>
  87.  
  88. <!-- inputy -->
  89. <form action='' method="POST">
  90. <div class="pole1">
  91. <input type="text" name="o1" size="5"/>
  92. </div>
  93.  
  94. <div class="pole2">
  95. <input type="text" name="o2" size="5"/>
  96. </div>
  97.  
  98. <div class="pole3">
  99. <input type="text" name="o3" size="5"/>
  100. </div>
  101.  
  102. <div class="licz">
  103. <input type="submit" value="Licz" />
  104. </div>
  105. </form>
  106. <p class="crownanie" id="ix0"></p>
  107. <p class="cx1" id="ix1"></p>
  108. </div>
  109. <script>
  110. document.querySelector('form').addEventListener('submit', (event) => {
  111.     // preventing default page reload
  112.     event.preventDefault()
  113.     const { target } = event
  114.     // caching DOM
  115.     const A = parseFloat(target['o1'].value)
  116.     const B = parseFloat(target['o2'].value)
  117.     const C = parseFloat(target['o3'].value)
  118.     // math operations
  119.     const delta = Math.pow(B, 2) - 4 * A * C
  120.     const sqrt = Math.sqrt(delta, 2)
  121.     // three cases scenario
  122.     let txt = {};
  123.     if (delta > 0) {
  124.         const x1 = (- B - sqrt)/(2 * A)
  125.         const x2 = (B - sqrt)/(2 * A)
  126.         txt['msg'] = "Delta dodatnia, dwa rozwiązania. &Delta; = " + delta
  127.         txt['x1'] = x1
  128.         txt['x2'] = x2
  129.         txt['count'] = 2
  130.     } else if (delta < 0) {
  131.         txt['msg'] = "Delta ujemna, brak rozwiązań. &Delta; = " + delta
  132.         txt['count'] = false
  133.     } else {
  134.         const x = -B/(2 * A)
  135.         txt['msg'] = "Delta daje dokładnie jedno rozwiązanie. &Delta; = " + delta
  136.         txt['x1'] = x
  137.         txt['count'] = 1
  138.     }
  139.     // caching DOM
  140.     const P1 = document.querySelector('#ix0')
  141.     const P2 = document.querySelector('#ix1')
  142.     // return text strings
  143.     P1.innerHTML = `f(x) =
  144.     ${A > 0 ? `${A}` : `- ${Math.abs(A)}`}x<sup>2</sup>
  145.     ${B > 0 ? `+ ${B}` : `- ${Math.abs(B)}`}x
  146.     ${C > 0 ? `+ ${C}` : `- ${Math.abs(C)}`}`
  147.     P2.innerHTML = txt['msg']
  148. })
  149. </script>
  150. </body>
  151. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement