Membuat Calculator Sederhana
cal.js
function proses(){
var myForm=document.form1;
if (myForm.operasi.value == "+"){
myForm.hasil.value = parseInt(myForm.bil1.value) + parseInt(myForm.bil2.value);
}
else if (myForm.operasi.value == "-"){
myForm.hasil.value = parseInt(myForm.bil1.value) - parseInt(myForm.bil2.value);
}
else if (myForm.operasi.value == "*"){
myForm.hasil.value = parseInt(myForm.bil1.value) * parseInt(myForm.bil2.value);
}
else {
myForm.hasil.value = parseInt(myForm.bil1.value) / parseInt(myForm.bil2.value);
}
}
cal.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple Calculator</title>
<script language="JavaScript" type="text/javascript" src="cal.js"></script>
</head>
<body bgcolor="#4380d4">
<center>
<h1> SIMPLE CALCULATOR</h1>
<form name=form1 action="#">
<table border="1">
<tr>
<td>Bilangan1 <input type="text" name="bil1"/></td>
</tr>
<tr>
<td align="center"><select name="operasi" >
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</td>
</tr>
<tr>
<td>Bilangan2 <input type="text" name="bil2" /></td>
</tr>
<tr>
<td align="center"><input type="button" value="=" onclick="proses()" /></td>
</tr>
<tr>
<td>Hasil
<input type="text" name="hasil" /></td>
</tr>
</table>
</form>
<br>
© alifable
</center>
</body>
</html>
Tidak ada komentar:
Posting Komentar