Search code examples
javascriptfunctionif-statementparseint

Not sure what is wrong with the function


I am completely new to this and am trying to write a program which will take inputs on a webpage and score the results in an output box. I am not sure what the problem is with this set of javascript, though I am sure that I am missing an integral piece! Any help is much appreciated!

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

Solution

  • You have several syntax errors:

    1. You are missing global parens in some if statements with multiple conditions, it should be like this:

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

    2. You are also missing a final }:

    Here is the final fixed code:

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