Studi Kasus
Script
hitung.js
function hitung(){
var komponen = document.calc;
var bil1 = parseInt(komponen.bil1.value);
var bil2 = parseInt(komponen.bil2.value);
var op = komponen.op.value;
var hasil;
if(op == "add"){
hasil = bil1 + bil2;
}
else if(op == "sub"){
hasil = bil1 - bil2;
}
else if(op == "mpy"){
hasil = bil1 * bil2;
}
else{
hasil = bil1 / bil2;
}
komponen.hasil.value = hasil;
}
kalkulator.html
CTYPE html>
<html lang="en">
<head>
<title>Kalkulator</title>
<script language="JavaScript" type="text/javascript" src="hitung.js"></script>
</head>
<body bgcolor="lightblue">
<center>
<h3>Simple Calculator</h3>
<form name="calc" action="#">
<input type="text" size="5" name="bil1">
<select name="op">
<option value="mpy">*</option>
<option value="div">/</option>
<option value="add">+</option>
<option value="sub">-</option>
</select>
<input type="text" size="5" name="bil2">
<input type="button" value="=" onclick="hitung()"/>
<input type="text" size="10" name="hasil" readonly>
</form>
</center>
</body>
</html>
Hasil tampilan
Penambahan
Pengurangan
Perkalian
Pembagian
Tidak ada komentar:
Posting Komentar