Selasa, 01 Maret 2011

Kalkulator Sederhana

MEMBUAT APLIKASI KALKULATOR SEDERHANA MENGGUNAKAN JAVASCRIPT




JAVASCRIPT
function hitung(){
    var myForm = document.form1;
    var a=eval(myForm.a.value);
    var b=eval(myForm.b.value);
    var pil= myForm.opt.value;
        if (pil == "tambah") {
    var c = a + b;
        }else if (pil == "kurang") {
    var c = a - b;
        } else if (pil == "kali") {
    var c = a * b;
        } else if (pil == "bagi") {
    var c = a / b;
        }
        myForm.hasil.value = c;
    }
   
function resetForm(){
    document.form1.reset();
}



HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Studi Kasus</title>
    <script type="text/javascript" src="math.js"></script>
</head>

<body>

<form name="form1" action="#">
<input type="text" name="a"/>
<select name="opt">
    <option value="tambah">+</option>
    <option value="kurang">-</option>
    <option value="kali">*</option>
    <option value="bagi">/</option>
</select>
<input type="text" name="b"/>
<input type="button" value="=" onClick="hitung()"/>
<input type="text" name="hasil" disabled="true"/><br/>
<input type="button" value="clear" onClick="resetForm()"/>
</form>

</body>
</html>






0 komentar:

Posting Komentar