Thursday, 25 December 2014
DASAR-DASAR PEMROGRAMAN DI JAVASCRIPT
.1 Operasi dasar aritmatika
<HTML>
<HEAD> <TITLE>Contoh Program Javascript</TITLE>
</HEAD>
<SCRIPT language="Javascript">
function test (val1,val2)
document.write("<br>"+"Perkalian : val1*val2 "+"<br>") document.write(val1*val2)
document.write("<br>"+"Pembagian : val1/val2 "+"<br>") document.write(val1/val2)
document.write("<br>"+"Penjumlahan : val1+val2 "+"<br>") document.write(val1+val2)
document.write("<br>"+"Pengurangan : val1-val2 "+"<br>") document.write(val1-val2)
document.write("<br>"+"Modulus : val1%val2 "+"<br>") document.write(val1%val2)
</SCRIPT>
<BODY>
<input type="button" name="button1" value="arithmetic" onclick=test(9,4)>
</BODY>
</HTML>
2. Operasi relational
<HTML> <HEAD>
<TITLE>Contoh Program Javascript</TITLE>
</HEAD>
<SCRIPT language="Javascript">
function test () {
val1=window.prompt("Nilai I :")
val2=window.prompt("Nilai II :")
document.write("<br>"+"val1==val2"+"<br>")
document.write(val1==val2)
document.write("<br>"+"val1!=val2"+"<br>")
document.write(val1!=val2)
document.write("<br>"+"val1>val2"+"<br>")
document.write(val1>val2)
document.write("<br>"+"val1<val2"+"<br>")
document.write(val1<val2) }
</SCRIPT>
<BODY>
<input type="button" name="button1" value="relational" onclick=test()>
</BODY>
</HTML>
3. Seleksi kondisi (if..else)
<HTML><HEAD>
<TITLE>Contoh if-else</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE = "JavaScript">
<!--
var nilai = prompt("Nilai (0-100): ", 0);
var hasil = "";
if (nilai >= 60)
hasil = "Lulus";
else
hasil = "Tidak Lulus";
document.write("Hasil: " + hasil);
//-->
</SCRIPT>
</BODY> </HTML>
4.Penggunaan operator switch untuk seleksi kondisi
<HTML> <HEAD>
<TITLE>Contoh Program Javascript</TITLE>
</HEAD>
<SCRIPT language="Javascript">
function test ()
val1=window.prompt("Input Nilai (1-5):")
switch (val1)
case "1" :
document.write("bilangan satu")
break
case "2" :
document.write("bilangan dua")
break
case "3" :
document.write("bilangan tiga")
break
case "4" :
document.write("bilangan empat")
break
case "5" :
document.write("bilangan lima")
break
default :
document.write("bilangan lainnya")
</SCRIPT>
<BODY>
<input type="button" name="button1" value="switch" onclick=test()>
</BODY>
</HTML>
5. Pemakaian looping < for >
<HTML> <HEAD>
<TITLE>Contoh Program Javascript</TITLE> </HEAD>
<BODY>
<SCRIPT language="Javascript">
<!--
for (x=0;x<=10;x++)
document.write(x+"<br>")
// -->
</SCRIPT>
</BODY>
</HTML>
6. Pemakaian looping < do..while >
<HTML> <HEAD>
<TITLE>Contoh Program Javascript</TITLE>
</HEAD>
<BODY>
<SCRIPT language="Javascript">
<!--
var x=0
do{
document.write(x+"<br>")
x++;
while (x<=10)
// -->
</SCRIPT>
</BODY>
</HTML>
7.Pemakaian looping < while >
<HTML> <HEAD>
<TITLE>Contoh Program Javascript</TITLE>
</HEAD>
<BODY>
<SCRIPT language="Javascript">
<!--
var x=0
while (x<=10){
document.write(x+"<br>")
x++;
// -->
</SCRIPT>
</BODY>
</HTML>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment