我试图添加一个 onclick 事件,以便它将第三个学生信息添加到控制台日志并存储它,同时还在 HTML 文件中显示下一个学生信息。我也试图让 gpa 的平均值也更新。
(function () {
var students = [{
name: 'Joshua Velez',
address: {
street: '123 Some Street ',
city: 'NoWhere, ',
state: 'Texas'},
gpa: [2.5, 3.2, 2.8]
},
{
name: 'John Doe',
address: {
street: '456 Any Street ',
city: 'AnyWhere, ',
state: 'Florida'},
gpa: [2.8, 3.5, 3.3]
}];
/*Displays first two student info in console.*****/
for(var x=0; x<students.length; x++){
console.log("Name: " + students[x].name);
console.log("Address: "+students[x].address.street + students[x].address.city + students[x].address.state);
console.log("GPA: "+students[x].gpa.join(","));
console.log("Date: "+date());
}
//Displays first students name and information in HTML onload.
function disFsInfo(){
document.getElementById('name').innerHTML = "Name: " + students[0].name;
document.getElementById('address').innerHTML ="Address: " +students[0].address.street + students[0].address.city + students[0].address.state;
document.getElementById('gpa').innerHTML="GPA: " + students[0].gpa.join(", ");
document.getElementById('date').innerHTML = "Date: " + date();
document.getElementById('gpaavg').innerHTML="Average GPA: " + " "
}
function date(){
var d = new Date();
var month=[];
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
return month[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear()
}
window.onload = disFsInfo;
})();