不确定该功能有什么问题

时间:2015-01-07 20:51:23

标签: javascript function if-statement parseint

我对此完全陌生,我正在尝试编写一个程序,该程序将在网页上获取输入并在输出框中对结果进行评分。我不确定这套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 个答案:

答案 0 :(得分:0)

您有几个语法错误:

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

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

  2. 您还错过了最后的}

  3. 这是最终的固定代码:

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