Created
March 3, 2016 11:10
-
-
Save joynal/06c8e76ce6bf8d80b9e9 to your computer and use it in GitHub Desktop.
Adding numbers, multiple times
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Html Calculator</title> | |
</head> | |
<body> | |
<div id="output"></div> | |
<div> | |
<label for="num_1">First Input</label> | |
<input type="number" id="num_1"> | |
</div> | |
<div> | |
<label for="num_2">Second Input</label> | |
<input type="number" id="num_2"> | |
</div> | |
<div> | |
<input type="submit" value="Add" onclick="addNumber()"> | |
</div> | |
<script> | |
var result = 0; | |
function addNumber () { | |
var num_1 = +document.getElementById('num_1').value; | |
var num_2 = +document.getElementById('num_2').value; | |
var output = document.getElementById('output'); | |
console.log(num_1); | |
if (num_1 && num_2) { | |
result += num_1 + num_2; | |
output.innerHTML = result; | |
} else if(num_1 || num_2){ | |
result += num_1 + num_2; | |
output.innerHTML = result; | |
} else{ | |
output.innerHTML = "Insert number"; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment