Algorithma Jadwal Shift Satpam dengan Javascript
Sebuah sekolah memiliki tiga unit pendidikan SD, SMP, dan SMA.
Masing-masing unit, memiliki empat orang Satpam yang berjaga bergantian 24 jam, selama 7 hari kerja, yaitu jam kerja pagi(6:00-14:00), siang(14:00-22:00), malam(22:00-6:00), dan libur.
Sedangkan pergantian tiap Satpam, dari jam kerja satu ke yang lain, misalnya dari jam kerja pagi ke jam kerja siang, dilakukan tiap dua hari sekali.
Sehingga tiap jam kerja terdapat tiga orang Satpam yang bertugas, dan setelah mendapat piket masuk malam, Satpam yang bersangkutan mendapat libur dua hari.
Kode di bawah ini adalah kode Javascript implementasi kasus pergantian jam kerja di atas.
Di mana input tanggal perlu diubah dulu ke bentuk Julian Date, dan dengan memperhatikan kondisi tanggal 25 Maret 2021 () sebagai patokan awal perhitungan.
<html> <head> <title>Contoh Piket Satpam</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <script> function Julian() { this.numday = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; this.nameday = ["Senin", "Selasa", "Rabu", "Kamis", "Jumat","Sabtu","Minggu"]; this.namemonth = ["Januari", "Februari", "Maret", "April", "Mei","Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"]; this.AwalTahun = -4712; this.peryear = 0; this.permonth = 0; this.penambah = 10; this.init=function() { for (let i = 0; i < this.numday.length; i++) { this.peryear += this.numday[i]; } this.permonth = this.peryear / this.numday.length; } this.trace=function(day,month,year) { if (month < 1 || month > 12) { if (month < 1) { month = 1; } if (month > 12) { month = 12; } } if (day < 1) { day = 1; } if(day>this.numday[month-1]){ day=this.numday[month-1]; } if(this.basitoh(year) && month==2){ day=this.numday[month-1]+1; } if (year == 1582 && month == 10) { if (day >= 5 && day < 15) { day = 15; } } this.day = day; this.month = month; this.year = year; var hasil = 0; for (let th = this.AwalTahun; th < year; th++) { if (th % 4 == 0) { if (!this.basitoh(th)) { hasil += 1; } else { if (th < 1582) { hasil += 1; } } } hasil += this.peryear; } var tam=0; if (year % 4 == 0) { if (!this.basitoh(year)) { tam++; } else { if (year < 1582) { tam++; } } } for (let i = 0; i < (month - 1); i++) { hasil += this.numday[i]; if(i==1)hasil+=tam; } hasil += day; if (year > 1582) { hasil -= this.penambah; } else if (year == 1582) { if (month > 10) { hasil -= this.penambah; } else if (month == 10) { if (day>=15) { hasil -= this.penambah; } } } jd = hasil-1; return jd; } this.basitoh=function(th) { return ((th >= 1582) && (th % 100 == 0) && (th % 400) != 0); } this.getNameDayOfWeek=function(jd) { return this.nameday[(jd% 7)]; } } function hitung() { let jul=new Julian(); jul.init(); var date = new Date(document.getElementById("tanggal").value); var day = date.getDate(); var month = date.getMonth() + 1; var year = date.getFullYear(); var jd=jul.trace(day,month,year); if(jd<2459299){ alert("Cannot calculate: must be more than 25 March 2021"); return; } document.getElementById("julianday").innerHTML=jul.getNameDayOfWeek(jd); var namaSatpam=["Dani","Taufik","Trico","Anshori","Bambang","Suli","Yuli","Wahyu","Risavi","Roni","Saiful","Hari"]; var a=4-parseInt((jd-2459299)/2)%4; var txt="<ul>"; txt+="<li>"+namaSatpam[3*(a%4)+0]+"</li>"; txt+="<li>"+namaSatpam[3*(a%4)+1]+"</li>"; txt+="<li>"+namaSatpam[3*(a%4)+2]+"</li>"; txt+="</ul>"; document.getElementById("pagi").innerHTML=txt; a++; txt="<ul>"; txt+="<li>"+namaSatpam[3*(a%4)+0]+"</li>"; txt+="<li>"+namaSatpam[3*(a%4)+1]+"</li>"; txt+="<li>"+namaSatpam[3*(a%4)+2]+"</li>"; txt+="</ul>"; document.getElementById("siang").innerHTML=txt; a++; txt="<ul>"; txt+="<li>"+namaSatpam[3*(a%4)+0]+"</li>"; txt+="<li>"+namaSatpam[3*(a%4)+1]+"</li>"; txt+="<li>"+namaSatpam[3*(a%4)+2]+"</li>"; txt+="</ul>"; document.getElementById("malam").innerHTML=txt; a++; txt="<ul>"; txt+="<li>"+namaSatpam[3*(a%4)+0]+"</li>"; txt+="<li>"+namaSatpam[3*(a%4)+1]+"</li>"; txt+="<li>"+namaSatpam[3*(a%4)+2]+"</li>"; txt+="</ul>"; document.getElementById("libor").innerHTML=txt; } function getDate() { var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; //January is 0! var yyyy = today.getFullYear(); if(dd<10) { dd = '0'+dd } if(mm<10) { mm = '0'+mm } today = yyyy + '-' + mm + '-' + dd; document.getElementById("tanggal").value = today; } window.onload = function() { getDate(); hitung(); }; </script> </head> <body> <div style="max-width:250px;margin:auto;padding: 20px 50;border: 1px solid #dedede;border-radius: 15px;"> <div style="margin:20px 0;"><span id="julianday"></span>, <input id="tanggal" type="date" onchange="hitung()"/></div> <h2>Pagi</h2> <div id="pagi"></div> <h2>Siang</h2> <div id="siang"></div> <h2>Malam</h2> <div id="malam"></div> <h2>Libor</h2> <div id="libor"></div> <div style="padding-top: 10px;border-top: 1px solid #dedede;"><a href="http://edugameapp.com">edugameapp.com</a></div> </div> </body> </html>
Implementasi dalam bentuk kode Javascript ini, dapat dilihat juga di link piketsatpam.html .
Komentar
Posting Komentar