function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const EXAMS_PER_DAY = 2;
const TOTAL_EXAMS = 5;
var exam_days = {
00: 0,
10: 0,
20: 0,
30: 0,
40: 0,
50: 0,
01: 0,
11: 0,
21: 0,
31: 0,
41: 0,
51: 0
};
var exam_array = [];
function generateSchedule() {
exam_array = [];
var c = [[0,1,2],[0,1,2,3,4,5],[0,1]];
for (var i = 0; i < c[0].length; i++) {
for (var j = 0; j < c[1].length; j++) {
for (var k = 0; k < c[2].length; k++) {
b = `${c[0][i]}${c[1][j]}${c[2][k]}`;
document.getElementById('exam-' + b).style.background = 'transparent';
}
}
}
for (var i = 0; exam_array.length < TOTAL_EXAMS; i++) {
var time = getRandomInt(0, 2);
var day = getRandomInt(0, 5);
var week = getRandomInt(0, 1);
var exam_id = "exam-" + time + day + week;
if (exam_days[day + '' + week] < EXAMS_PER_DAY && !exam_array.includes(exam_id)) {
exam_days[day + '' + week] += 1;
exam_array.push(exam_id);
document.getElementById(exam_id).style.background = "green";
}
}
}
table,
th,
td {
border: 1px solid black;
}
<table>
<tr>
<th></th>
<th scope="col">M</th>
<th scope="col">T</th>
<th scope="col">W</th>
<th scope="col">R</th>
<th scope="col">F</th>
<th scope="col">S</th>
</tr>
<tr>
<th scope="row">Morning</th>
<td id="exam-000"></td>
<td id="exam-010"></td>
<td id="exam-020"></td>
<td id="exam-030"></td>
<td id="exam-040"></td>
<td id="exam-050"></td>
</tr>
<tr>
<th scope="row">Afternoon</th>
<td id="exam-100"></td>
<td id="exam-110"></td>
<td id="exam-120"></td>
<td id="exam-130"></td>
<td id="exam-140"></td>
<td id="exam-150"></td>
</tr>
<tr>
<th scope="row">Evening</th>
<td id="exam-200"></td>
<td id="exam-210"></td>
<td id="exam-220"></td>
<td id="exam-230"></td>
<td id="exam-240"></td>
<td id="exam-250"></td>
</tr>
</table>
<table>
<tr>
<th></th>
<th scope="col">M</th>
<th scope="col">T</th>
<th scope="col">W</th>
<th scope="col">R</th>
<th scope="col">F</th>
<th scope="col">S</th>
</tr>
<tr>
<th scope="row">Morning</th>
<td id="exam-001"></td>
<td id="exam-011"></td>
<td id="exam-021"></td>
<td id="exam-031"></td>
<td id="exam-041"></td>
<td id="exam-051"></td>
</tr>
<tr>
<th scope="row">Afternoon</th>
<td id="exam-101"></td>
<td id="exam-111"></td>
<td id="exam-121"></td>
<td id="exam-131"></td>
<td id="exam-141"></td>
<td id="exam-151"></td>
</tr>
<tr>
<th scope="row">Evening</th>
<td id="exam-201"></td>
<td id="exam-211"></td>
<td id="exam-221"></td>
<td id="exam-231"></td>
<td id="exam-241"></td>
<td id="exam-251"></td>
</tr>
</table>
<br />
<button onclick="generateSchedule();">Generate</button>
<button onclick="location.reload(true);">Reload</button>
<button onclick="window.print();">Print</button>
提供的jsfiddle。 –
codepen提供,友好的用戶 – Sophie
您可以檢查'window.location.search' for'reload',然後執行函數 –