290 likes | 428 Views
CONTOH LATIHAN JAVASCRIPT. <html> <p><b> Program Fungsi </b></p><hr> <head> <script language ="JavaScript"> function nama() { alert ("Hallo nama saya …. "); } </script> </head> <body> <script language ="JavaScript"> nama(); </script> </body> </html>. CONTOH fungsi. <html>
E N D
<html> <p><b> Program Fungsi </b></p><hr> <head> <script language ="JavaScript"> function nama() { alert ("Hallo nama saya…."); } </script> </head> <body> <script language ="JavaScript"> nama(); </script> </body> </html> CONTOH fungsi
<html> <head> <script language="JavaScript"> function pesan() { alert("Hallo sayaaktifdari even onload"); } </script> </head> <body onload ="pesan()"> </body> </html> fungsi
<html> <head> <script language ="JavaScript"> function cetak(txt) { alert("Hallo " +txt) } function konfirmasi() { vartanya=confirm(“APAKAH INGIN MENAMBAH DATA LAGI ?"); if (tanya==true) { alert(“SILAHKAN ISI KEMBALI..!"); } else { alert(“TERIMA KASIH..!"); } } function masuk() { alert("Program iniberisicontoh\n"+ "membuatsuatukejadian\n\n"+ "SelamatBelajar ...!"); } Contoh fungsi
function keluar() { alert("Terimakasihanda\n"+ "Telahmencoba program ini"); } </script> </head> <body onload="masuk()" onunload="keluar()"> <form> nama :<input type="text" name="nama" size=10> <input type="button" value="KIRIM" onclick="cetak(nama.value)"><hr> <a href="" onclick="konfirmasi(); return false;">KIRIM KE SISTEM</a> </form> </body> </html> sambungan
<html> <head> <script language="JavaScript"> function inputpass() { varbaris=0; do { var pass = prompt("Masukkan Password",""); if(pass =="JOKER") { alert ("Selamat Password Benar"); break; } else { alert("Maaf Password Salah"); if(baris==3) { alert("Maafkesempatanandahanya 3 x"); } baris=baris+1; } } Contoh FUNGSI
while(baris<3) } </script> </head> <body> <form> <input type="button" value="password" onclick="inputpass()"> </form> </body> </html> SAMBUNGAN
<html> <head> <script language="JavaScript"> function inputpass(pass) { varbaris=0; do { // var pass = prompt("Masukkan Password",""); if(pass =="JOKER") { alert ("Selamat Password Benar"); break; } else { alert("Maaf Password Salah"); if(baris==3) { alert("Maafkesempatanandahanya 3 x"); } baris=baris+1; break; } } FUNGSI
while(baris<3) } </script> </head> <body> <form> password : <input type="text" name="pass" size=10> <input type="button" value="KIRIM" onclick="inputpass(pass.value)"> </form> </body> </html> SAMBUNGAN
<html> <head> <script language="JavaScript"> function cek() { var nim1 = document.fmform.nim.value; var nama1 = document.fmform.nama.value; var alamat1 = document.fmform.alamat.value; if(nim1=="") { alert("NIM harusdiisi ..!"); document.fmform.nim.focus(); return false; } FUNGSI
else { if(nama1=="") { alert("NAMA harusdiisi ..!"); document.fmform.nama.focus(); return false; } else { if(alamat1=="") { alert("ALAMAT harusdiisi ..!"); document.fmform.alamat.focus(); return false; } else { alert("NIM :"+nim1+"\n"+ "Nama :"+nama1+"\n"+ "Alamat :"+alamat1); return true; } } } } SAMBUNGAN
</script> </head> <body> <form name="fmform"> NIM :<input type="text" name="nim" size=10><br> Nama :<input type="text" name="nama" size=10><br> Alamat :<input type="text" name="alamat" size=10><hr> <input type="button" value="PROSES" onclick="cek()"> </form> </body> </html> SAMBUNGAN
<html> <body> <script language ="JavaScript"> var a = 9 ; var b = 4 ; document.write ("Membandingkan 2 bilangan <hr>"); if (a > b) { document.write("bilangan = "+a+"lebihbesardari "+b); } if (a < b) { document.write("bilangan = "+b+"lebihkecildari "+a); } </script> </body> </html> KONDISI IF..
<html> <body> <script language ="JavaScript"> var a = 9; selisih = 0; var b = 4; document.write ("bilangan a ="+a+"<br>"); document.write ("bilangan b ="+b+"<br>"); if (a>b) { selisih = a - b; document.write ("Bilangan = "+a+"lebihbesardari ="+b); document.write ("<br>Selisihnya ="+selisih); }else{ selisih = b - a ; document.write ("Bilangan = "+a+"lebihkecildari ="+b); document.write ("<br>Selisihnya ="+selisih); } </script> </body> </html> KONDISI IF....ELSE
<html> <body> <script language ="JavaScript"> var a = -9; document.write ("bilangan ="+a+"<br>"); if (a>0) { document.write (a+"adalahbilangan POSITIP"); }else{ document.write (a+"adalahbilangan NEGATIP"); } </script> </body> </html> KONDISI IF..ELSE
<html> <body> <script language ="JavaScript"> var a = 0; document.write ("bilangan ="+a+"<br>"); if (a>0) { document.write (a+"adalahbilangan POSITIP"); }else{ if (a<0) { document.write (a+"adalahbilangan NEGATIP"); } else { document.write ("Bilangan NOL"); } } </script> </body> </html> KONDISI IF...ELSE
<html> <body> <script language ="JavaScript"> var nil = 55; document.write ("bilangan ="+nil+"<br>"); if (nil>=85) { document.write ("Grade A <br>"); document.write ("LULUS"); }else{ if (nil>=70 && nil<85) { document.write ("Grade B <br>"); document.write ("LULUS"); } else { if (nil>=60 && nil<70) { document.write ("Grade C <br>"); document.write ("LULUS"); } KONDISI IF...ELSE
else { if (nil>40 && nil <60) { document.write ("Grade D <br>"); document.write ("GAGAL"); } else { document.write("GRADE E <br>"); document.write("GAGAL"); } } } } </script> </body> </html> SAMBUNGAN
<html> <head> <script language ="JavaScript"> function cabang() { var a = parseFloat(document.fmform.bilangan1.value) ; var b = parseFloat(document.fmform.bilangan2.value) ; if(isNaN(a)) { a =0.0; } if(isNaN(b)) { b =0.0; } if (a > b) { alert("bilangan = "+a+"lebihbesardari "+b); } if (a < b) { alert("bilangan = "+a+"lebihkecildari "+b); } } KONDISI IF
</script> </head> <body> <form name ="fmform"> Bilangan 1 : <input type ="text" name="bilangan1" size=5><br> Bilangan 2 : <input type ="text" name="bilangan2" size=5><hr> <input type ="button" value="Bandingkan" onClick ="cabang()"> </form> </body> </html> SAMBUNGAN
<html> <body> <b> cetakangka 0 - 9 dengan do..while </b><hr> <script language = "JavaScript"> var x = 0; do { document.write(x+"<br>"); x++; } while (x <= 9) </script> </body> </html> DO...WHILE
<html> <body> <b> cetakangka 0 - 9 dengan while </b><hr> <script language = "JavaScript"> var x = 0; while (x <= 9) { document.write(x+"<br>"); x++; } </script> </body> </html> WHILE
<html> <body> <script language ="JavaScript"> vartanya = true while(tanya==true) { varangka = prompt("MasukkanAngka :",""); for(x=1;x<=angka;x++) { for(j=1;j<=x;j++) { document.write("*"); } document.write("<br>"); } document.write("<hr>PiramidaBintang ="+angka+"<br>"); vartanya = confirm("Apakahakanlanjut ?") if (tanya==true) {continue;} else {break;} } </script> </body> </html> GABUNGAN WHILE, FOR DAN IF
<html> <body> <script language ="JavaScript"> vartanya = true ; var h=0; var c=0; while(tanya==true) { varangka = prompt("MasukkanAngka :",""); for(x=1;x<=angka;x++) { h=c+x; for(j=1;j<=h;j++) { document.write(" * "); } document.write("<br>"); c=c+1; } CONTOH 2
document.write("<hr>PiramidaBintang ="+angka+"<br>"); vartanya = confirm("Apakahakanlanjut ?") if (tanya==true) {continue;} else {break;} } </script> </body> </html> SAMBUNGAN
<html> <body> <script language ="JavaScript"> document.write("<b> Cetakangka 1 - 9 </b><hr>"); for (X=1 ; X<=9 ; X+=2) { document.write(X+" | "); } </script> </body> </html> FOR
<html> <body> <script language ="JavaScript"> document.write("<b> Cetakangka 1 - 9 </b><hr>"); vargenap=0; vartgenap=0; varganjil=0; vartganjil=0; var total=0; for (X=1 ; X<=10 ; X++) { document.write(X+" | "); if((X%2)==0) { genap=genap+1; tgenap=tgenap+X; } FOR
else { ganjil =ganjil+1; tganjil=tganjil+X; } total = total+X; } document.write("<br>Total Bilangan="+total+"<br>\n"+ "ganjilada="+ganjil+"totalnyaada ="+tganjil+"<br>\n"+ "genapada ="+genap+"totalnyaada="+tgenap); </script> </body> </html> SAMBUNGAN