不确定函数有什么问题

Not sure what is wrong with the function

本文关键字:问题 什么 函数 不确定      更新时间:2023-09-26

我对此完全陌生,正在尝试编写一个程序,该程序将在网页上输入并在输出框中对结果进行评分。我不确定这组javascript有什么问题,尽管我确定我错过了一个完整的部分!任何帮助都非常感谢!

function laten() { 
var Q2 = document.getElementById('twoScore').value;
if (Q2 == "") {
    Q2 = 0;}
var Q2new = 0;
if (parseInt(Q2) >= 0) && (parseInt(Q2) <= 15) {
    Q2new = 0;
} else if (parseInt(Q2) > 15) && (parseInt(Q2) <=30) {
    Q2new = 1;
} else if (parseInt(Q2) > 30) && (parseInt(Q2) <=60) {
    Q2new = 2;
} else if (parseInt(Q2) > 60) {
    Q2new = 3;
}
document.getElementById('latency').value = Q2new;
var Q5a = document.getElementById('fiveaScore').value;
if (Q5a == "") {
    Q5a = 0;}
var latenAdd = parseInt(Q5a) + parseInt(Q2new);
if (latenAdd == "") {
    latenAdd = 0;}
var latenScore = 0;
if (latenScore == "") {
    latenScore = 0;}

if (latenAdd == 0) {
        latenScore = 0;
    } else if ((latenAdd >= 1) && (latenAdd <= 2)) {
        latenScore = 1;
    } else if ((latenAdd >= 3) && (latenAdd <= 4)) {
        latenScore = 2;
    } else if ((latenAdd >= 1) && (latenAdd <= 2)) {
        latenScore = 3;
    }

if (!isNaN(latenScore)) {
        document.getElementById('latency').value = latenScore;
}

您有几个语法错误:

  1. 您在具有多个条件的某些if语句中缺少全局括号,它应该是这样的:

    if ( (condition 1) && (condition2))

  2. 您还缺少最后的}

这是最终的固定代码:

function laten() { 
    var Q2 = document.getElementById('twoScore').value;
    if (Q2 == "") {
        Q2 = 0;
    }
    var Q2new = 0;
    if ((parseInt(Q2) >= 0) && (parseInt(Q2) <= 15)) {
        Q2new = 0;
    } else if ((parseInt(Q2) > 15) && (parseInt(Q2) <=30)) {
        Q2new = 1;
    } else if ((parseInt(Q2) > 30) && (parseInt(Q2) <=60)) {
        Q2new = 2;
    } else if (parseInt(Q2) > 60) {
        Q2new = 3;
    }
    document.getElementById('latency').value = Q2new;
    var Q5a = document.getElementById('fiveaScore').value;
    if (Q5a == "") {
        Q5a = 0;
    }
    var latenAdd = parseInt(Q5a) + parseInt(Q2new);
    if (latenAdd == "") {
        latenAdd = 0;
    }
    var latenScore = 0;
    if (latenScore == "") {
        latenScore = 0;
    }
    if (latenAdd == 0) {
        latenScore = 0;
    } 
    else if ((latenAdd >= 1) && (latenAdd <= 2)) {
        latenScore = 1;
    } 
    else if ((latenAdd >= 3) && (latenAdd <= 4)) {
        latenScore = 2;
    } 
    else if ((latenAdd >= 1) && (latenAdd <= 2)) {
        latenScore = 3;
    }
    if (!isNaN(latenScore)) {
        document.getElementById('latency').value = latenScore;
    }
}