在表中打印数组?Javascript/HTML
在这个项目中,我必须做一个日记,其中一个标准是一个表格,它记录了以前输入的预约次数。目前,这是我的剧本,但它的工作,当我有多年,理货只承认第一年,不理货第二年。相反,它将其识别为NaN。有人能帮我指出我哪里出了问题吗。非常感谢:)在表中打印数组?Javascript/HTML,javascript,html,arrays,Javascript,Html,Arrays,在这个项目中,我必须做一个日记,其中一个标准是一个表格,它记录了以前输入的预约次数。目前,这是我的剧本,但它的工作,当我有多年,理货只承认第一年,不理货第二年。相反,它将其识别为NaN。有人能帮我指出我哪里出了问题吗。非常感谢:) 施工最低年限=2019年; var日期=[]; var-priArr=[]; 风险值年数=[]; var列表=[]; 让预约={ 日期:“, 开始时间:“, 结束时间:“, 主题:“, 地点:“, rowData:函数(){ 返回“+this.date+”“+thi
施工最低年限=2019年;
var日期=[];
var-priArr=[];
风险值年数=[];
var列表=[];
让预约={
日期:“,
开始时间:“,
结束时间:“,
主题:“,
地点:“,
rowData:函数(){
返回“+this.date+”“+this.startTime+”“+this.endTime+”“+this.subject+”“+this.venture+”“+this.priority+”;
}
}
让yearsum={
年份:“,
伯爵:“是的。”
}
/*
年份:“,
计数:“,
行年:函数(){
返回“+年[i]+”+计数[i]+”;
}
*/
函数addAppointment(){
const isDateValid=validDate()
让newAppointment=Object.create(约会);
var startIndex=document.getElementById('startTime')。selectedIndex;
var优先级;
newAppointment.startTime=document.getElementById('startTime')。选项[startIndex]。值;
newAppointment.subject=document.getElementById('subject')。值;
newAppointment.venture=document.getElementById('venture')。值;
如果(isDateValid!=false){newAppointment.date=isDateValid;}
如果(validTime!=false){newAppointment.endTime=validTime();}
如果(document.getElementById('medium').checked){priority=medium.value;}
else if(document.getElementById('low').checked){priority=low.value;}
else{priority=high.value;}
newAppointment.priority=优先级;
如果(isDateValid==null | isDateValid==undefined | validTime()==null | validTime()==undefined | validTime()==false | isDateValid==false | IsConcurrentAppoint(newAppointment.date,newAppointment.startTime,newAppointment.endTime)!=true){
警报('日期或时间输入无效')
}
否则{
列表。推送(新约会);
优先推送(优先级);
日期。推送(isDateValid)
让tbody=document.getElementById(“tbody”);
tbody.innerHTML+=newAppointment.rowData();
}
}
函数validDate(){
var inputDate=document.getElementById('date')。值
var dateformat=/^(0?[1-9]|[12][0-9]|[3[01])[\/\-](0?[1-9]|[012])[\/\-]\d{4}$/;
if(inputDate.match(日期格式)){
document.getElementById('date').focus();
var opera1=inputDate.split('/');
var opera2=inputDate.split('-');
lopera1=操作长度;
lopera2=opera2.长度;
如果(1>1){
var pdate=inputDate.split('/');
}
否则如果(lopera2>1){
var pdate=inputDate.split('-');
}
var dd=parseInt(pdate[0]);
var mm=parseInt(pdate[1]);
var yy=parseInt(pdate[2]);
如果(yy 2){
如果(dd>daysInMonths[mm-1]){
警报('日期无效');
返回误差;
}
}
如果(mm==2){
var-lyear=false;
如果(!(yy%4)和&yy%100)| |!(yy%400)){
lyear=正确;
}
如果((lyear==false)和&(dd>=29)){
警报(“日期无效”);
返回误差;
}
如果((lyear==true)和&(dd>29)){
警报('日期无效');
返回误差;
}
}
}
否则{
警报('日期无效');
document.getElementById('date').focus();
返回误差;
}
返回输入日期;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
函数validTime(){
var startIndex=document.getElementById('startTime')。selectedIndex;
var appStartTime=document.getElementById('startTime')。选项[startIndex]。值;
var endIndex=document.getElementById('endTime')。selectedIndex;
var endTime=document.getElementById('endTime')。选项[endIndex]。值;
if(endTime列表[i].endTime.substring(0,3)
||startInput.substring(0,3)==list[i]。startTime.substring(0,3)和&endInput.substring(0,3)>=list[i]。endTime.substring(0,3)
||startInput.substring(0,3)>列表[i]。startTime.substring(0,3)和&endInput.substring(0,3)>列表[i]。endTime.substring(0,3)和&startInput.substring(0,3)list[i].startTime.substring(0,3)&&endInput.substring(0,3)list[i]。startTime.substring(0,3)
||startInput.substring(0,3)>=list[i].endTime.substr
<html>
<head>
<script>
const MIN_YEAR = 2019;
var dates = [];
var priArr = [];
var years = [];
var list = [];
let appointment = {
date: "",
startTime: "",
endTime: "",
subject: "",
venue: "",
rowData: function () {
return "<tr><td>" + this.date + "</td><td>" + this.startTime + "</td><td>" + this.endTime + "</td><td>" + this.subject + "</td><td>" + this.venue + "</td><td>" + this.priority + "</td></tr>";
}
}
let yearSumm = {
year: "",
count: ""
}
/*
year: "",
count: "",
rowYear: function () {
return "<tr><td>" + years[i] + "</td><td>" + count[i] + "</td></tr>";
}
*/
function addAppointment() {
const isDateValid = validDate()
let newAppointment = Object.create(appointment);
var startIndex = document.getElementById('startTime').selectedIndex;
var priority;
newAppointment.startTime = document.getElementById('startTime').options[startIndex].value;
newAppointment.subject = document.getElementById('subject').value;
newAppointment.venue = document.getElementById('venue').value;
if (isDateValid != false) { newAppointment.date = isDateValid; }
if (validTime != false) { newAppointment.endTime = validTime(); }
if (document.getElementById('medium').checked) { priority = medium.value; }
else if (document.getElementById('low').checked) { priority = low.value; }
else { priority = high.value; }
newAppointment.priority = priority;
if (isDateValid === null | isDateValid === undefined | validTime() === null | validTime() === undefined | validTime() === false | isDateValid === false | isConcurrentAppointment(newAppointment.date, newAppointment.startTime, newAppointment.endTime) != true) {
alert('Date or time entry invalid')
}
else {
list.push(newAppointment);
priArr.push(priority);
dates.push(isDateValid)
let tbody = document.getElementById("tbody");
tbody.innerHTML += newAppointment.rowData();
}
}
function validDate() {
var inputDate = document.getElementById('date').value
var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
if (inputDate.match(dateformat)) {
document.getElementById('date').focus();
var opera1 = inputDate.split('/');
var opera2 = inputDate.split('-');
lopera1 = opera1.length;
lopera2 = opera2.length;
if (lopera1 > 1) {
var pdate = inputDate.split('/');
}
else if (lopera2 > 1) {
var pdate = inputDate.split('-');
}
var dd = parseInt(pdate[0]);
var mm = parseInt(pdate[1]);
var yy = parseInt(pdate[2]);
if (yy <= MIN_YEAR) {
alert('Date is invalid.');
return error;
}
var daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (mm == 1 || mm > 2) {
if (dd > daysInMonths[mm - 1]) {
alert('Date is invalid.');
return error;
}
}
if (mm == 2) {
var lyear = false;
if ((!(yy % 4) && yy % 100) || !(yy % 400)) {
lyear = true;
}
if ((lyear == false) && (dd >= 29)) {
alert('Date is invalid');
return error;
}
if ((lyear == true) && (dd > 29)) {
alert('Date is invalid.');
return error;
}
}
}
else {
alert('Date is invalid.');
document.getElementById('date').focus();
return error;
}
return inputDate;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function validTime() {
var startIndex = document.getElementById('startTime').selectedIndex;
var appStartTime = document.getElementById('startTime').options[startIndex].value;
var endIndex = document.getElementById('endTime').selectedIndex;
var endTime = document.getElementById('endTime').options[endIndex].value;
if (endTime <= appStartTime) {
alert("Appointment must end later than the start time.");
return error;
}
return endTime;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function isConcurrentAppointment(dateInput, startInput, endInput) {
var i;
for (i = 0; i < list.length; i++) {
if (dateInput == list[i].date) {
if (startInput.substring(0, 3) == list[i].startTime.substring(0, 3)
|| startInput.substring(0, 3) < list[i].startTime.substring(0, 3) && endInput.substring(0, 3) > list[i].endTime.substring(0, 3)
|| startInput.substring(0, 3) == list[i].startTime.substring(0, 3) && endInput.substring(0, 3) >= list[i].endTime.substring(0, 3)
|| startInput.substring(0, 3) > list[i].startTime.substring(0, 3) && endInput.substring(0, 3) > list[i].endTime.substring(0, 3) && startInput.substring(0, 3) < list[i].endTime.substring(0, 3)
|| startInput.substring(0, 3) > list[i].startTime.substring(0, 3) && endInput.substring(0, 3) < list[i].endTime.substring(0, 3)
|| startInput.substring(0, 3) < list[i].startTime.substring(0, 3) && endInput.substring(0, 3) < list[i].endTime.substring(0, 3) && endTime.substring(0, 3) > list[i].startTime.substring(0, 3)
|| startInput.substring(0, 3) >= list[i].endTime.substring(0, 3) && endInput.substring(0, 3) <= list[i].endTime.substring(0, 3)
|| startInput.substring(0, 3) > list[i].startTime.substring(0, 3) && endInput.substring(0, 3) == list[i].endTime.substring(0, 3)
|| startInput.substring(0, 3) == list[i].startTime.substring(0, 3) && endInput.substring(0, 3) == list[i].endTime.substring(0, 3)
|| startInput.substring(0, 3) < list[i].startTime.substring(0, 3) && endInput.substring(0, 3) == list[i].endTime.substring(0, 3)) {
alert('Appointments cannot start of end while another appointment is running');
return false
}
}
else
return true
}
return true
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Tallying function:
var i = 0;
var count = 0;
var output = '';
var tally = new Array();
// Tally of years
function summary(year) {
var val = document.getElementById('tallyOption').value
if (val == 'year') {
thead1.innerHTML = ("<td>" + 'Year' + "</td>" + "<td>" + 'Appointments' + "</td>");
for (i = 0; i < years.length; i++) {
tally[i] = 0;
}
for (i = 0; i < dates.length; i++) {
var year = dates[i].toString().split('/')
var year = parseInt(year[2]);
if (years.indexOf(year) == -1) {
years.push(year);
tally[i] = 1;
}
else {
tally[i] +=1;
}
}
output += '<table border="1">';
output += '<tr><th>Years</th><th>Tally</th></tr>';
for (i = 0; i < years.length; i++) {
output += '<tr><td>' + years[i] + '</td><td>';
for (var j = 0; j < tally[i]; j++) {
output += '$';
}
output += '</td></tr>';
}
output += '</table>';
document.writeln(output);
return year;
return count;
}
//Tallying Dates?
}
</script>
</head>
<body>
<title>Diary</title>
<h1 style="text-align: center;">Diary</h1>
<form>
<table bgcolor="#cccccc" cellpadding="5" cellspacing="0" align="center">
<tr>
<td align="right">Date</td>
<td><input type="text" id="date" name="date" size="10"></td>
<td align="right">Start Time</td>
<td>
<select id="startTime">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
<option value="17:00">17:00</option>
<option value="18:00">18:00</option>
</select>
</td>
<td align="right">End Time</td>
<td>
<select id="endTime">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
<option value="17:00">17:00</option>
<option value="18:00">18:00</option>
</select>
</td>
</tr>
<tr>
<td align="right">Subject:</td>
<td><input type="text" id="subject" size="10"></td>
</tr>
<tr>
<td align="right">Venue:</td>
<td><input type="text" id="venue" size="10"></td>
</tr>
<tr>
<td valign="top" align="center">Priority</td>
<td><input type="radio" id="high" name="Priority" value="High" checked="true" /> High<br />
<td><input type="radio" id="medium" name="Priority" value="Medium" /> Medium<br />
<td><input type="radio" id="low" name="Priority" value="Low" /> Low<br />
</td>
</tr>
</table>
<tr>
<div style="text-align:center;">
<td></td>
<td></td><input type="button" value="Add Appointment" onclick="addAppointment()" /></td>
</div>
</tr>
<hr>
<div>
<table align="center" width="80%" height="150px" cellpadding="1px" cellspacing="1px" border="1" id="table1">
<thead>
<tr>
<th width="50">Date</th>
<th width="20">Start</th>
<th width="20">End</th>
<th width="75">Subject</th>
<th width="60">Venue</th>
<th width="5">Priority</th>
</tr>
</thead>
<tbody id="tbody"> </tbody>
</table>
</div>
<tr>
<td></td>
<div style="text-align:center;">
<td></td><input type="reset" value="Randomise Appointments" onclick="shuffleAppointments()" /></td>
<td></td><input type="button" value="Sort Appointments" onclick="sortRecords()" /></td>
<td>by</td>
<td>
<select id="Date">
<option value="date">Date</option>
<option value="startTime">Start Time</option>
<option value="endTime">End Time</option>
<option value="subject">Subject</option>
<option value="venue">Venue</option>
<option value="priority">Priority</option>
</select>
</div>
</td>
</tr>
<hr>
<div>
<table border="1">
<thead id="thead1">
</thead>
<tbody id="tbody2"></tbody>
</table>
</div>
<div style="text-align:left;">
<td></td><input type="button" value="Summary" onclick="summary()" /></td>
<td>by</td>
<td>
<select id="tallyOption">
<option value="year">Year</option>
<option value="date">Date</option>
<option value="optPriority">Priority</option>
</select>
</div>
</hr>
</form>
function summary() {
var year;
var val = document.getElementById('tallyOption').value
if (val == 'year') {
thead1.innerHTML = ("<td>" + 'Year' + "</td>" + "<td>" + 'Appointments' + "</td>");
for (i = 0; i < dates.length; i++) {
const date = new Date(dates[i])
const year = date.getFullYear()
if (!yearSum[year]) {
yearSum[year] = 1
} else {
yearSum[year] = yearSum[year] + 1
}
}
output += '<table border="1">';
output += '<tr><th>Years</th><th>Tally</th></tr>';
const keys = Object.keys(yearSum)
const values = Object.values(yearSum)
for (i = 0; i < keys.length; i++) {
output += '<tr><td>' + keys[i] + '</td><td>';
output += values[i]
output += '</td></tr>';
}
output += '</table>';
document.writeln(output);
return year;
return count;
}
//Tallying Dates?
}
const yearSum = {}