我一直收到这个错误,让我很困惑......
function calculate(){
var n = document.getElementById("noOfCourses").value;
for(var i = 0 ; i < n ; i++) {
var course[i] = document.getElementById("GPA" + i+1).value;
var hours[i] = document.getElementById("hours" + i+1).value;
// Calculate the product of Course GPA and Credit Hours
var product[i] = course[i] * hours[i];
}
}
答案 0 :(得分:0)
基本上你需要在使用它们之前声明和初始化数组。
function calculate(){
var n = document.getElementById("noOfCourses").value,
course = [], // declare and init before
hours = [], // declare and init before
product = []; // declare and init before
for(var i = 0 ; i < n ; i++) {
course[i] = document.getElementById("GPA" + i+1).value;
hours[i] = document.getElementById("hours" + i+1).value;
// Calculate the product of Course GPA and Credit Hours
product[i] = course[i] * hours[i];
}
}
答案 1 :(得分:0)
var
关键字用于声明新变量,并可选择初始化它们。它不用于普通作业。在声明的变量中包含索引是没有意义的 - 索引用于访问数组的内容,而不是声明任何内容。
function calculate(){
var n = document.getElementById("noOfCourses").value;
for(var i = 0 ; i < n ; i++) {
course[i] = document.getElementById("GPA" + i+1).value;
hours[i] = document.getElementById("hours" + i+1).value;
// Calculate the product of Course GPA and Credit Hours
product[i] = course[i] * hours[i];
}
}