为什么在我的列表中创建了5个元素?需要创建理货功能。JavaScript

为什么在我的列表中创建了5个元素?需要创建理货功能。JavaScript,javascript,html,arrays,function,Javascript,Html,Arrays,Function,主要关注点是在JS中,“添加约会”功能是循环的,“年份”值以5个元素的列表结束,即使只进行了一次约会。我认为两个额外的是从add appointment函数创建的,另外两个是从concurrent appointment函数创建的。这是一个小组项目,我没有做这个组件,这就是为什么我很难理解和解决这个问题 我试图将yy变量放入年份列表中,然后创建一个计数函数来列出每年有多少约会 非常感谢您的建议!如果我还不够详细,请告诉我!:) 提前谢谢 <html> <head>

主要关注点是在JS中,“添加约会”功能是循环的,“年份”值以5个元素的列表结束,即使只进行了一次约会。我认为两个额外的是从add appointment函数创建的,另外两个是从concurrent appointment函数创建的。这是一个小组项目,我没有做这个组件,这就是为什么我很难理解和解决这个问题

我试图将yy变量放入年份列表中,然后创建一个计数函数来列出每年有多少约会

非常感谢您的建议!如果我还不够详细,请告诉我!:)

提前谢谢

    <html>

<head>
    <script>
        const MIN_YEAR = 2019;
        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>";
            }
        }

        function addAppointment() {
            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 (validDate() != false) { newAppointment.date = validDate(); }

            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 (validDate() === null | validDate() === undefined | validTime() === null | validTime() === undefined | validTime() === false | validDate() === false | isConcurrentAppointment(newAppointment.date, newAppointment.startTime, newAppointment.endTime) != true) {
                alert('Date or time entry invalid')
            }
            else {
                list.push(newAppointment);
                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]);
                years.push(yy);
                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)) {
                        alert('New appointments cannot start before previous one finishes');
                        return false
                    }
                    else
                        return true
                }
                else
                    return true
            }
            return true
        }

        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        //Tallying function:
        var i;

        //var yearArr = new Array(years[i]);

        function summary() {
            document.writeln('<table border="1">')
            document.writeln('<tr><th>Year</th><th>Count</th></tr>')
            for (i = 0; i < years.length; i++) {
                document.writeln('<tr><td>' + years[i] + '</td>'
                    + '<td>' + searchCount(years[i], years) + '</td></tr>')

            }
            document.writeln('</table>')


            function searchCount(targetValue, arr) {
                var count = 0; // Count of target value
                var i = 0;     // Iterative counter

                // Examine each element.
                for (i = 0; i < arr.length; i++) {
                    if (arr[i] == targetValue)
                        count++;
                }
                return count;
            }
        }





    </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 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>

</body>

施工最低年限=2019年;
风险值年数=[];
var列表=[];
让预约={
日期:“,
开始时间:“,
结束时间:“,
主题:“,
地点:“,
rowData:函数(){
返回“+this.date+”“+this.startTime+”“+this.endTime+”“+this.subject+”“+this.venture+”“+this.priority+”;
}
}
函数addAppointment(){
让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')。值;
如果(validDate()!=false){newAppointment.date=validDate();}
如果(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=优先级;
如果(validDate()==null | validDate()==undefined | validTime()==null | validTime()==undefined | validTime()==false | validDate()==false | isConcurrentAppoint(newAppointment.date,newAppointment.startTime,newAppointment.endTime)!=true){
警报('日期或时间输入无效')
}
否则{
列表。推送(新约会);
让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);
如果(年<最小年){
警报('日期无效');
返回误差;
}
var daysInMonths=[31,28,31,30,31,30,31,31,30,31];
如果(mm==1 | | mm>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)            if (validDate() != false) { newAppointment.date = validDate(); }
const isDateValid = validDate()
            const isDateValid = validDate()
            if (isDateValid != false) { newAppointment.date = isDateValid; }

and 

            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')
            }