Advertisement
ShadowEmbrace

zad_6

Sep 11th, 2024 (edited)
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.71 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7. </head>
  8. <body>
  9.     <input id="num1" type="number" placeholder="first number">
  10.     <input id="num2" type="number" placeholder="second number">
  11.  
  12.     <button onclick="sumInputs()">Sum</button>
  13.     <button onclick="subtractInputs()">Subtract</button>
  14.     <button onclick="multiplyInputs()">Multiply</button>
  15.     <button onclick="divideInputs()">Divide</button>
  16.  
  17.     <p id="result"></p>
  18.  
  19.     <script>
  20.         function getData(){
  21.             const num1 = Number(document.getElementById('num1').value)
  22.             const num2 = Number(document.getElementById('num2').value)
  23.             let paragraph = document.getElementById('result')
  24.             return {number1: num1, number2: num2, paragraphElement: paragraph}
  25.         }
  26.  
  27.         function sumInputs(){
  28.             let data = getData()
  29.             let sum = data.number1 + data.number2
  30.             data.paragraphElement.innerHTML = `The sum is: ${sum}`
  31.         }
  32.  
  33.         function subtractInputs(){
  34.             let data = getData()
  35.             let result = Math.abs(data.number1 - data.number2)
  36.             data.paragraphElement.innerHTML = 'The diff is: ' + result
  37.         }
  38.  
  39.         function multiplyInputs(){
  40.             let data = getData()
  41.             let result = data.number1 * data.number2
  42.             data.paragraphElement.innerHTML = result
  43.         }
  44.        
  45.         function divideInputs(){
  46.             let data = getData()
  47.             let result = data.number1 / data.number2
  48.             data.paragraphElement.innerHTML = result
  49.         }
  50.        
  51.     </script>
  52. </body>
  53. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement