Tutorial Programming

Tutorial Perhitungan Sederhana Dengan JavaScript


Hallo sahabat yang baca tutorial ini, saya akan menjelaskan bagaimana perhitungan sederhana mengunakan javascript, seperti kalkulator, berikut codingnya. Terimakasih.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript - Studi Kasus Modul 5</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
function hitung(){
var x=eval(document.form1.x.value);
var y=eval(document.form1.y.value);
var pil= document.form1.opt.value;

if (pil == "tambah") {
var z = x + y;
}
else if (pil == "kurang") {
var z = x - y;
}
else if (pil == "kali") {
var z = x * y;
}
else if (pil == "bagi") {
var z = x / y;
}
document.form1.hasil.value = z;
}
function resetForm(){
document.form1.reset();
}
//-->
</script>
<form name="form1" action="#">

<input type="text" name="x" />
<select name="opt">
<option value="tambah"> + </option>
<option value="kurang"> - </option>
<option value="kali"> * </option>
<option value="bagi"> / </option>
</select>
<input type="text" name="y" />
<input type="button" value="=" onClick="hitung()" />
<input type="text" name="hasil" disabled="true" /><br/>
<input type="button" value="Reset" onClick="resetForm()" />
</form>
</body>
</html>

No comments:

Post a Comment

Tutorial Programming Designed by Templateism | Ary EJFA Copyright © 2015

Theme images by richcano. Powered by Blogger.