我不断收到此错误,这使我感到困惑。

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];
    }
}

最佳答案

基本上,您需要在使用数组之前声明和初始化数组。

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];
    }
}

关于javascript - Javascript SyntaxError : missing ; before statement (after for loop),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44077088/

10-12 12:51