ivana_andreevska

Kalkulator(so funkcii)

Nov 5th, 2021
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //HTML del
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <script src="script.js"></script>
  7.     <link rel="stylesheet" href="style.css">
  8.     <title>Kalkulator</title>
  9. </head>
  10. <body>
  11.  
  12. <form>
  13.     Value 1: <input type="text" id="value1">
  14.     <br>
  15.     Value 2: <input type="text" id="value2">
  16.     <br><br>
  17.     Operator:
  18.  
  19.     <select id="operator">
  20.         <option value="a">Add</option>
  21.         <option value="s">Subtract</option>
  22.         <option value="m">Multiply</option>
  23.         <option value="d">Divide</option>
  24.     </select>
  25.  
  26.     <button type="button" onclick="calc()">Calculate</button>
  27. </form>
  28.  
  29. <div id="result">
  30. </div>
  31. </body>
  32. <script src="script.js"></script>
  33. </html>
  34.  
  35.  
  36. //JavaScript Del
  37. function calc()
  38. {
  39.     //select a specific element based on class or id
  40.     //so parseInt konvertirame od string vo int
  41.     var a=parseInt(document.querySelector("#value1").value);
  42.     var b=parseInt(document.querySelector("#value2").value);
  43.     var op=document.querySelector("#operator").value;
  44.     var calculate;
  45.  
  46.     if(op==="a")
  47.     {
  48.         calculate=a+b;
  49.     }
  50.     else if(op==="s")
  51.     {
  52.         calculate=a-b;
  53.     }
  54.     else if(op==="m")
  55.     {
  56.         calculate=a*b;
  57.     }
  58.     else if(op==="d")
  59.     {
  60.         calculate=a/b;
  61.     }
  62.  
  63.     document.querySelector("#result").innerHTML=calculate;
  64.     //inserts a vlue inbetween HTML tags
  65.  
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment