Skip to content

Instantly share code, notes, and snippets.

@jerry-maheswara-github
Created January 21, 2017 13:16
Show Gist options
  • Save jerry-maheswara-github/128fea038d60a2efa67e1a9a107bf5f3 to your computer and use it in GitHub Desktop.
Save jerry-maheswara-github/128fea038d60a2efa67e1a9a107bf5f3 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>
:: Latihan Javascript ::
</title>
</head>
<body>
<h1>Simple Calculator</h1>
<form>
<table border="1" cellpadding="4px" cellspacing="6px">
<tr>
<td colspan="4" align="center"> <input type="text" id="angka" value="" > </td>
</tr
<tr>
<td align="center"><input type="button" value="1" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="2" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="3" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="+" onclick="pencet(this)"/></td>
</tr>
<tr>
<td align="center"><input type="button" value="4" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="5" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="6" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="-" onclick="pencet(this)"/></td>
</tr>
<tr>
<td align="center"><input type="button" value="7" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="8" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="9" onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="*" onclick="pencet(this)"/></td>
</tr>
<tr>
<td align="center"><input type="button" value="0" onclick="pencet(this)"/></td>
<td align="center"><input type="reset" value="C" id="clear" /></td>
<td align="center"><input type="button" value="." onclick="pencet(this)"/></td>
<td align="center"><input type="button" value="/" onclick="pencet(this)"/></td>
</tr>
<tr>
<td colspan=4>
<input type=button value="=" onclick="result()" >
</td>
</tr>
</table>
</form>
<style type="text/css">
#angka{
font-size: 30pt;
text-align: right;
width: 220px;
}
input[type="button"], input[type="reset"]{
width: 100%;
}
</style>
<script>
var angka = '';
var hasil = 0;
function pencet(e){
if (hasil > 0 || document.getElementById('angka').value == 0){
document.getElementById('angka').value = '';
hasil = 0;
}
angka = e.value;
document.getElementById('angka').value += e.value ;
angka += angka;
}
function result(){
angka2 = document.getElementById('angka').value;
hasil = eval(angka2);
if (hasil !== undefined) {
document.getElementById('angka').value = hasil;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment