javascript - Javascript SyntaxError : missing ; before statement (after for loop)

标签 javascript for-loop error-handling syntax-error

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

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/

相关文章:

javascript - 在 Razor 页面中使用 OnPost 页面处理程序处理 AJAX 请求

javascript - 返回新数组的对象数组之间的差异

java - For 循环由于某种原因不会添加术语

php - 如果检测到php fatal error ,如何配置nginx转到静态html页面?

asp.net - 集中式 ASP.NET 错误处理 : <customErrors> vs. Application_Error。可以同时使用吗?

jsf - JSF错误页面消息

Javascript 在旧浏览器中执行点击 html "image"

javascript - 如何使 Div 中的文本不可选择/不可复制?

javascript - 如何使用 lodash _.filter 与 _.forEach 重构 for 循环?

php mysql 用for循环保存mysql