berikut adalah kodenya :
<html>
<head>
<title>Belajar Kalkulator</title>
<script language = "javaScript">
function tambah(input, character)
{
if(input.value == null || input.value == "0")
input.value = character
else
input.value += character
}
function ubah_tanda(input)
{
if(input.value.substring(0,1) == "-")
input.value = input.value.substring(1, input.value.length)
else
input.value = "-" + input.value
}
function hitung(form)
{
form.display.value = eval(form.display.value)
}
</script>
</head>
<body>
<center>
<h1>Kalkulator JavaScript</h1><hr>
<form>
<table border="10" align=center cellpadding=5 cellspacing=0)
<!---baris ke-1>
<tr>
<td colspan = 4 align=center>
<input name = "display" value="0" size=20>
</td>
</tr>
<!---baris ke-2>
<tr>
<td>
<input type = button value = " 7 " onClick="tambah(this.from.display, '7')">
</td>
<td>
<input type = button value = " 8 " onClick="tambah(this.from.display, '8')">
</td>
<td>
<input type = button value = " 9 " onClick="tambah(this.from.display, '9')">
</td>
<td>
<input type = button value = " / " onClick="tambah(this.from.display, '/')">
</td>
</tr>
<!---baris ke-3>
<tr>
<td>
<input type = button value = " 4 " onClick="tambah(this.from.display, '4')">
</td>
<td>
<input type = button value = " 5 " onClick="tambah(this.from.display, '5')">
</td>
<td>
<input type = button value = " 6 " onClick="tambah(this.from.display, '6')">
</td>
<td>
<input type = button value = " * " onClick="tambah(this.from.display, '*')">
</td>
</tr>
<!---baris ke-4>
<tr>
<td>
<input type = button value = " 1 " onClick="tambah(this.from.display, '1')">
</td>
<td>
<input type = button value = " 2 " onClick="tambah(this.from.display, '2')">
</td>
<td>
<input type = button value = " 3 " onClick="tambah(this.from.display, '3')">
</td>
<td>
<input type = button value = " - " onClick="tambah(this.from.display, '-')">
</td>
</tr>
<!--baris ke-5>
<tr>
<td>
<input type = button value = " 0 " onClick="tambah(this.from.display, '0')">
</td>
<td>
<input type = button value = " . " onClick="tambah(this.from.display, '.')">
</td>
<td>
<input type = button value = " +/- " onClick="ubah_tanda(this.from.display)">
</td>
<td>
<input type = button value = " + " onClick="tambah(this.from.display, '+')">
</td>
</tr>
<!--baris ke-6>
<tr>
<td colspan = "2">
<input type = button value = " hitung! "name = "enter" onClick ="hitung(this.from)">
</td>
<td colspan = "2">
<input type = button value = " C " onClick="this.form.display.value = 0 ">
</td>
</tr>
</table>
</form>
</body>
</html>
berikut adalah keluaran dari program tersebut :
selamat mencoba kawan semoga berhasil....
<html>
<head>
<title>Belajar Kalkulator</title>
<script language = "javaScript">
function tambah(input, character)
{
if(input.value == null || input.value == "0")
input.value = character
else
input.value += character
}
function ubah_tanda(input)
{
if(input.value.substring(0,1) == "-")
input.value = input.value.substring(1, input.value.length)
else
input.value = "-" + input.value
}
function hitung(form)
{
form.display.value = eval(form.display.value)
}
</script>
</head>
<body>
<center>
<h1>Kalkulator JavaScript</h1><hr>
<form>
<table border="10" align=center cellpadding=5 cellspacing=0)
<!---baris ke-1>
<tr>
<td colspan = 4 align=center>
<input name = "display" value="0" size=20>
</td>
</tr>
<!---baris ke-2>
<tr>
<td>
<input type = button value = " 7 " onClick="tambah(this.from.display, '7')">
</td>
<td>
<input type = button value = " 8 " onClick="tambah(this.from.display, '8')">
</td>
<td>
<input type = button value = " 9 " onClick="tambah(this.from.display, '9')">
</td>
<td>
<input type = button value = " / " onClick="tambah(this.from.display, '/')">
</td>
</tr>
<!---baris ke-3>
<tr>
<td>
<input type = button value = " 4 " onClick="tambah(this.from.display, '4')">
</td>
<td>
<input type = button value = " 5 " onClick="tambah(this.from.display, '5')">
</td>
<td>
<input type = button value = " 6 " onClick="tambah(this.from.display, '6')">
</td>
<td>
<input type = button value = " * " onClick="tambah(this.from.display, '*')">
</td>
</tr>
<!---baris ke-4>
<tr>
<td>
<input type = button value = " 1 " onClick="tambah(this.from.display, '1')">
</td>
<td>
<input type = button value = " 2 " onClick="tambah(this.from.display, '2')">
</td>
<td>
<input type = button value = " 3 " onClick="tambah(this.from.display, '3')">
</td>
<td>
<input type = button value = " - " onClick="tambah(this.from.display, '-')">
</td>
</tr>
<!--baris ke-5>
<tr>
<td>
<input type = button value = " 0 " onClick="tambah(this.from.display, '0')">
</td>
<td>
<input type = button value = " . " onClick="tambah(this.from.display, '.')">
</td>
<td>
<input type = button value = " +/- " onClick="ubah_tanda(this.from.display)">
</td>
<td>
<input type = button value = " + " onClick="tambah(this.from.display, '+')">
</td>
</tr>
<!--baris ke-6>
<tr>
<td colspan = "2">
<input type = button value = " hitung! "name = "enter" onClick ="hitung(this.from)">
</td>
<td colspan = "2">
<input type = button value = " C " onClick="this.form.display.value = 0 ">
</td>
</tr>
</table>
</form>
</body>
</html>
berikut adalah keluaran dari program tersebut :
selamat mencoba kawan semoga berhasil....
Comments
Post a Comment