Kalkulator dengan JavaScript


Berikut Code-nya:

<html>
<head>
<style type="text/css">
table, td, th
{
border:1px solid white;
background-color:black;
color:white;
}
</style>
<script type="text/javascript">
function tambah()
{
var x = document.test4.input.value;
var y = document.test4.input2.value;
document.test4.hasil.value = Number(x) + Number(y);
if(x =="" || y ==""){
alert("Tidak boleh Kosong");
}
else if (isNaN(document.test4.hasil.value))
alert("Inputan hanya berupa angka");
}
function kurang()
{
var x = document.test4.input.value;
var y = document.test4.input2.value;
document.test4.hasil.value = Number(x) - Number(y);
if(x =="" || y ==""){
alert("Tidak boleh Kosong");
}
else if (isNaN(document.test4.hasil.value))
alert("Inputan hanya berupa angka");
}
function bagi()
{
var x = document.test4.input.value;
var y = document.test4.input2.value;
document.test4.hasil.value = Number(x)/Number(y);
if(x =="" || y ==""){
alert("Tidak boleh Kosong");
}
else if (isNaN(document.test4.hasil.value))
alert("Inputan hanya berupa angka");
}
function kali()
{
var x = document.test4.input.value;
var y = document.test4.input2.value;
document.test4.hasil.value = Number(x) * Number(y);
if(x =="" || y ==""){
alert("Tidak boleh Kosong");
}
else if (isNaN(document.test4.hasil.value))
alert("Inputan hanya berupa angka");
}
function toHex()
{
var z=parseInt(document.test4.hasil.value);
document.test4.hasil2.value = z.toString(16);
}
function toOct()
{
var z=parseInt(document.test4.hasil.value);
document.test4.hasil2.value = z.toString(8);
}
function toBin()
{
var z=parseInt(document.test4.hasil.value);
document.test4.hasil2.value = z.toString(2);
}
</script>
</head>
<body>
<table align="center">
<th colspan="2">KALKULATOR</th>
<form name="test4">
<tr>
<td><input type="text" name="input"></td>
<td><input type="text" name="input2"></td>
</tr>
<tr>
<td><input type="button" onclick="tambah()" value="+"></td>
<td><input type="button" onclick="kurang()" value="-"></td>
</tr>
<tr>
<td><input type="button" onclick="kali()" value="x"></td>
<td><input type="button" onclick="bagi()" value=":"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="Reset" value="C">
</td>
</tr>
<tr>
<td>Resuilt</td>
<td><input type="text" name="hasil"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" onclick="toBin()" value="toBin">
<input type="button" onclick="toHex()" value="tohex">
<input type="button" onclick="toOct()" value="toOct"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="text" name="hasil2">
/td>
</tr>
</form>
</table>
</body>
</html>
Fungsi: +, - , : , x, c (clear/reset)
Kondisi Alert:
1. Jika inputan bukan angka dan;
2. Jika inputan kosong.
Konfersi : Biner(2), Octal(8), Hexa(16)


Tidak ada komentar:

Posting Komentar