Created
September 26, 2015 20:17
-
-
Save viggou/1d94c746106b97fe5239 to your computer and use it in GitHub Desktop.
Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html><head> | |
<meta charset="UTF-8"> | |
<title>Calculator</title> | |
<!-- Javascript calculator --> | |
<script> | |
function performOperation(operator) { | |
var x = parseInt(document.getElementById('txt1').value); | |
var y = parseInt(document.getElementById('txt2').value); | |
// Operations | |
var result; | |
switch(operator) { | |
case "+": | |
result = x + y; | |
break; | |
case "-": | |
result = x - y; | |
break; | |
case "*": | |
result = x * y; | |
break; | |
case "/": | |
if (y == 0) | |
result = "Error" | |
else | |
result = x / y; | |
break; | |
} | |
document.getElementById('result').value = result; | |
} | |
</script> | |
</head> | |
<body> | |
<h2 style="color:#FF0004"> How to use:</h2> | |
<p> Type in the <u>two</u> numbers that you would like calculate, <br> | |
then press one of the operators to get the result!</p> | |
<!-- Textfields and buttons --> | |
<input type="text" id="txt1" placeholder="1st. Number" autofocus=""> | |
<button onclick="performOperation('+')"> + </button> | |
<button onclick="performOperation('-')"> - </button> | |
<button onclick="performOperation('*')"> * </button> | |
<button onclick="performOperation('/')"> / </button> | |
<input type="text" id="txt2" placeholder="2nd. Number"> | |
<p> Result: </p> | |
<input type="text" id="result" placeholder="Don't type here"> <p> Author: <a href="https://youtube.com/user/yaypixxo">PIXX</a></p> | |
</body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment