Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Web tech</title>
- <script>
- function calculate(){
- var operation = document.getElementById("op").value;
- if (operation == "empty"){
- alert("Please select operation!");
- } else {
- calc(operation);
- }
- }
- function calc(operation){
- var num1 = parseFloat(document.getElementById("num1").value);
- var num2 = parseFloat(document.getElementById("num2").value);
- var result = 0;
- switch (operation){
- case '+': result = num1 + num2; break;
- case '-': result = num1 - num2; break;
- case '*': result = num1 * num2; break;
- case '/': result = num1 / num2; break;
- }
- document.getElementById("result").innerHTML = "Result: " + result;
- }
- function reset(){
- document.getElementById("num1").value = "";
- document.getElementById("num2").value = "";
- document.getElementById("result").innerHTML = "Result: ";
- document.getElementById("op").value = "empty";
- }
- </script>
- </head>
- <body>
- <h3>Simple Javascript calculator</h3>
- Number 1: <input type="text" id="num1"> <br>
- Number 2: <input type="text" id="num2"> <br>
- Operation:
- <select id="op">
- <option value="empty"></option>
- <option value="+">+</option>
- <option value="-">-</option>
- <option value="*">*</option>
- <option value="/">/</option>
- </select>
- </br>
- <button class="btn" onclick="calculate()">CALCULATE</button>
- <button class="btn" onclick="reset()">CLEAR</button>
- <br>
- <p id="result"> Result: </p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement