CONTOH PROGRAM JAVASCRIPT

<HTML>
<HEAD>
<TITLE>Persamaan kuadrat</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE = "JavaScript">
function det(a,b,c)
{
var d = (b*b)-(4*a*c);
return(d);
}
var a = parseFloat(prompt("Masukan Nilai A =\n"));
var b = parseFloat(prompt("Masukan Nilai B =\n"));
var c = parseFloat(prompt("Masukan Nilai C =\n"));
var deter = det(a,b,c);
document.write("Nilai Determinannya = " +deter+"<BR>");
document.write("<BR>");
document.write("<BR>");
if (deter>0)
{
var x1 = (-b - Math.sqrt(deter))/(2*a);
var x2 = (-b + Math.sqrt(deter))/(2*a);
document.write("Nilai x1 = " +x1+ "<BR>");
document.write("Nilai x2 = " +x2+ "<BR>");
document.write("<BR>");
document.write("<BR>");
document.write("Akar-akarnya Berbeda");
} else
if (deter==0)
{
var x1 = (-b - Math.sqrt(deter))/(2*a);
var x2 = (-b + Math.sqrt(deter))/(2*a);
document.write("Nilai x1 = " +x1+ "<BR>");
document.write("Nilai x2 = " +x2+ "<BR>");
document.write("<BR>");
document.write("<BR>");
document.write("Akar-akarnya Sama");
} else
if (deter<0)
{
document.write("Akar-akarnya Imaginer");
}
</SCRIPT>
</BODY>
</HTML>





<html>
<head>
<script>
<!--
function fibonacci(n){
var i;
var array_fib = new Array();
array_fib.push(0);
array_fib.push(1);
for(i=0; i<n; i++){
array_fib.push(array_fib[0] + array_fib[1]);
array_fib.shift();
}
return array_fib[0];
}
for(i=0; i<=20; i++)
document.write("Fibonacci(" + i + ") = " + fibonacci(i) + "<br>");
//-->
</script>
</head>
<body>
</body>
</html>


<HTML>
<HEAD>
<TITLE>Type Bilangan</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE = "JavaScript">
<!--
var
a = 15;
b = 14;
tambah = a + b;
kali = a * b;
document.write("Penambahan (" +a+ " + " +b+ ") = " + tambah);
document.write("<BR>");
document.write("Perkalian (" +a+ " x " +b+ ") = " + kali );
document.write("<BR>");
//-->
</SCRIPT>
</BODY>
</HTML>



<HTML>
<HEAD>
<TITLE>Membuat Fungsi Matematika Dengan JavaScript</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE = "JavaScript">
<!--
function jumlah(x, y) {
var hasil = x + y;
return(hasil);
}
var a = parseInt(prompt("Ketikan Bilangan A = \n Ingat Jangan String"));
var b = parseInt(prompt("Ketikan Bilangan B = \n Ingat Jangan String"));
var z = jumlah(a,b);
document.write(z);
document.write("<BR>");
document.write(“<hr>”;
document.write("Tipe Data nya adalah = " + typeof(z));
//-->
</SCRIPT>
</BODY>
</HTML>


<HTML>
<HEAD>
<TITLE>Belajar Array</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE = "JavaScript">
<!--
var
DATA_KU = new Array("Saya","Sedang","Senang");
for (i=0; i<3; i++)
{
document.write(DATA_KU[i] +"<br>");
}
alert ("Selamat Belajar Struktur Data");
alert ("Semoga Memperoleh Ilmu yang bermanfaat");
//-->
</SCRIPT>
</BODY>
</HTML>






<HTML>
<HEAD>
<TITLE>Parameter Array</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE = "JavaScript">
<!--
var
tempat = []);
var data = prompt("Ketik Jumlah Maksimum Array :");
for (m=1; m<=data; m++)
{
var bil = prompt("Masukan Bilangan :" )
tempat[m]= bil;
}
for (n=1; n<=data; n++)
{
document.write("Elemen Array ("+n+")
{
=" +tempat[n] + "<BR>");
//-->
</SCRIPT>
</BODY>
</HTML>




<html>
<head>
<script>
<!--
function fibonacci(n){
var i;
var array_fib = new Array();
array_fib.push(0);
array_fib.push(1);
for(i=0; i<n; i++){
array_fib.push(array_fib[0] + array_fib[1]);
array_fib.shift();
}
return array_fib[0];
}
for(i=0; i<=20; i++)
document.write("Fibonacci(" + i + ") = " + fibonacci(i) + "<br>");
//-->
</script>
</head>
<body>
</body>
</html>




<html>
</head><title >Membuat Rekaman Objek</title></head>
<body>
<script language = "JavaScript">
<!--
var Record = new Array();
function Mahasiswa(npm, nama, prodi, jenjang) {
// Properti
this.npm
= npm;
this.nama
= nama;
this.prodi
= prodi;
this.jenjang
= jenjang;
// Metode
this.LihatRecordMahasiswa = LihatRecordMahasiswa;
}
function LihatRecordMahasiswa() {
document.write("<td>" + this.npm + "</td>");
document.write("<td>" + this.nama + "</td>");
document.write("<td>" + this.prodi + "</td>");
document.write("<td>" + this.jenjang + "</td>");
}
//mengisi
Record[0]
Record[1]
Record[2]
Record[3]
Halaman .: 8 :.
data
= new
= new
= new
= new
Mahasiswa("43E001",
Mahasiswa("43E001",
Mahasiswa("43E001",
Mahasiswa("43E001",
"Dani",
"Doni",
"Dina",
"Dini",
STMIK Kharisma Karawang 2009
"TI",
"TI",
"TI",
"TI",
"S1");
"S1");
"S1");
"S1");
//menampilkan data
document.write("<table border = '1'>");
document.write("<th>NPM</th> <th>Nama</th>");
document.write("<th>Prodi</th><th>Jenjang</th> <br> ");
for (i=0; i < Record.length; i++)
{
document.write("<tr>");
Record[i].LihatRecordMahasiswa();
document.write("</tr>");
}
document.write("</table> <br>");
document.write("<table border = '1'>");
document.write("<th>NPM</th> <th>Nama</th>");
document.write("<th>Prodi</th><th>Jenjang</th> <br> ");
for (i=0; i < Record.length; i++)
{
document.write("<tr>");
Record[i].npm
document.write("</tr>");
}
document.write("</table>");
//-->
</script>
</body>
</html>



<html>
<head><title>Memanipulasi Data Pegawai</title>
<script language = "javascript" type="text/javascript">
<!--
var DBPegawai = new Array();
function Pegawai( Fields_NIP,
Fields_Nama,
Fields_Gaji_Per_Jam,
Fields_Jam_Kerja,
Fields_Gaji_Total)
{
this.NIP
= Fields_NIP;
this.Nama
= Fields_Nama;
this.Gaji_Jam
= Fields_Gaji_Per_Jam;
this.Jam_Kerja = Fields_Jam_Kerja;
this.Gaji_Total = Fields_Gaji_Total;
}
function inputPegawai()
{
var Nama,Gaji_Per_Jam, Jam_Kerja, Gaji ;
while (
NIP = window.prompt("Ketikan NIP (Nomor Induk Pegawai)
\n\n Click 'cancel' untuk Berhenti !!." ) )
{
Nama
= window.prompt( "Nama Pegawai :" ) ;
Gaji_Per_Jam
= parseInt(window.prompt( "Gaji Per Jam :" )) ;
Jam_Kerja
= parseInt(window.prompt( "Jumlah Jam Kerja :" )) ;
Gaji
= Gaji_Per_Jam * Jam_Kerja;
DBPegawai[DBPegawai.length] = new Pegawai(NIP,Nama,Gaji_Per_Jam,Jam_Kerja,Gaji );
}
ShowPegawai();
}
function ShowPegawai(){
document.write("<table border='1'>" +
"<tr>" +
"<th>NIP</th><th>Nama</th><th>Gaji Per Jam</th><th>Jam Kerja</th>”+
“<th>Gaji Total</th>" +
"</tr>");
for (i=0; i < DBPegawai.length; i++) {
document.write("<tr>" +
"
<td>" + DBPegawai[i].NIP + "</td>
"
<td>" + DBPegawai[i].Nama + "</td>" +
"
<td align='right'>" + DBPegawai[i].Gaji_Jam + "</td>" +
"
<td align='right'>" + DBPegawai[i].Jam_Kerja + "</td>" +
"
<td align='right'>" + DBPegawai[i].Gaji_Total + "</td>" +
"</tr>");
}
document.write( "</table>");
}
//-->
</script>
</head>
<body onload ="inputPegawai();">
</body>
</html>


<html>
<head>
<title>COntoh Implementasi Push dan POP pada Stack</title>
</head>
<body>
<script type="text/javascript">
var stack = new Array;
stack.push("A");
stack.push("B");
stack.push("C");
document.write("stack:"+stack.toString() +"<br>");
var YangDiAmbil = stack.pop();
document.write("Data Yang Diambil:"+YangDiAmbil);
document.write("<br>");
document.write("Isi Stack Terakhir:"+stack.toString());
</script>
</body>
</html>



<html>
<head>
<title>Struktur Fata stack</title>
<script language = "JavaScript">
var stack = new Array();
function Input_ke_Stack(data) {
stack.push(data);
}
function Ambil_dari_Stack() {
var data_dari_stack = stack.pop();
if (data_dari_stack == undefined)
return "Stack Sudah Kosong";
else
return data_dari_stack;
}
function TampilkanStack(list){
list.options.length = 0;
for (var i = 0; i < stack.length; i++){
var data = new Option(stack[i]);
list.options[list.options.length] = data;
}
}
</script>
</head>
<body>
<form>
<input type=text name=textSimpan>
<input type=button value="Masukan ke Stack"
onClick='Input_ke_Stack(textSimpan.value);
textSimpan.value="";
TampilkanStack(mediaList);'>
<select name="mediaList" size=12>
<option>Tampilan data....
</select>
<input type=text name=textAmbil size=20>
<input type=button value="Ambil dari Stack"
onClick="textAmbil.value = Ambil_dari_Stack();
TampilkanStack(mediaList);">
</form>
</body>
</html>




<html>
<head>
<title>Program Queue dengan shift dan unshift javaScript</title>
<script language="JavaScript">
function DataQueue()
{
var queue = [];
queue.unshift(1);
queue.unshift(2);
var i = queue.shift();
document.write(i + "<br/>");
queue.unshift(3);
queue.unshift(4);
var i = queue.shift();
document.write(i + "<br/>");
}
</script>
</head>
<body>
<form name="FormStrukturData">
<input name="btnQueue" value="Queue" type="button"
onClick="DataQueue();" />
</form>
</body>
</html>



<html>
<head>
<title>Struktur Data Queue</title>
<script language = "JavaScript">
var queue = new Array();
function Masuk_Antrian(data) {
stack.unshift(data);
}
function Keluar_Antrian() {
var yang_keluar_antrian = queue.shift();
if (queue.length == 0)
return "Stack Sudah Kosong";
else
return yang_keluar_antrian;
}
function Dalam_Antrian(list){
list.options.length = 0;
for (var i = 0; i < queue.length; i++){
var data = new Option(queue[i]);
list.options[list.options.length] = data;
}
}
</script>
</head>
<body>
<form>
<input type=text name=textSimpan>
<input type=button value="Masuk Antrian"
onClick='Masuk_Antrian(textSimpan.value);
textSimpan.value="";
Dalam_Antriaan(Visualisasi);'>
<select name="Visualisasi" size=12>
<option>Isi Antrian....
</select>
<input type=text name=textAmbil size=20>
<input type=button value="Keluar Antrian"
onClick="textAmbil.value = Keluar_Antrian();
Dalam_Antrian(Visualisasi);">
</form>
</body>
</html>



Sumber :
Diposkan oleh hadi wibowo di 18.54 
Kirimkan Ini lewat Email
BlogThis!
Berbagi ke Twitte