0

Please see my js validation function below.

function validate_submit(PassForm) {
    var bGo = false;
    var rankcount = document.getElementById('rankCount').value;

    var j = 0;

    var iRankcount0 = document.getElementById('indRankcount0').value;
    var iRankcount1 = document.getElementById('indRankcount1').value;
    var iRankcount2 = document.getElementById('indRankcount2').value;

    var ijs = 0;
    var itemp = ijs;

    for (i = 0; i < rankcount; i++) {
        alert("begin i = " + i);
        if (i == 0) {
            indRankcount = iRankcount0;
        }
        else if (i == 1) {
            alert('indRankcount: ' + indRankcount);
            indRankcount = iRankcount1;
            alert('iRankcount1: ' + iRankcount1);
            alert('indRankcount: ' + indRankcount);
        }
        else if (i == 2) {
            indRankcount = iRankcount2;
        }
        alert('before sec loop indRank: ' + indRankcount);
        alert('before sec loop itemp: ' + itemp);
        for (k = itemp; k < indRankcount; k++) {
            alert('in check bGo');
            if (document.getElementById("selectedScore" + i + k).checked) {
                bGo = true;
                j++;
            } //if
        } //for indRankcount - k loop

        if (bGo) {
            if (i == 0) {
                par = (Math.ceil(indRankcount / 4));
            }
            else if (i == 1) {
                par = (Math.ceil((iRankcount1 - iRankcount0) / 4));
                alert('1: ' + par);
            }
            else if (i == 2) {
                par = (Math.ceil((indRankcount2 - iRankcount1) / 4));
            }
            if (j == par) {
                j = 0;
                bGo = false;
                itemp = indRankcount;
                alert("itemp = " + itemp);
                continue;
            }
            else {
                alert('25% criteria not met.');
                return false;
            }
        }
        else { //else to check bGo
            alert('Atleast one box need to be selected.');
            return false;
        }
        j = 0;
        bGo = false;
        itemp = indRankcount;
        alert("loop ends: i =" + i);
    } //for rankcount - i loop

    res = window.confirm('Are you sure you want to proceed with the selection?');
    if (res) {
        return true;
    }
    else {
        return false;
    }
} //end of validate

Problem is when i=0, it executes fine. But when i=1, second loop (K) doesn't execute(we switched the variable to constant- it works for either itemp or indRankcount.Just one number did it.) It totally skips. Help please! Thank you!

5
  • Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? When asking for help, please take the time to format and indent your code in a consistent and reasonable fashion, out of respect for the time of the people you're asking to help you. (It's a good idea to do it anyway, for yourself, as well.) Commented Jan 19, 2018 at 13:04
  • I've run the code through jsbeautifier.org for you. (No affiliation.) Commented Jan 19, 2018 at 13:05
  • does it work fine for i==2? Commented Jan 19, 2018 at 13:06
  • 1
    By far the best way to understand what's going on is to use the powerful debugger built into your IDE or browser and step through the code statement by statement, watching the values of the variables, etc. Commented Jan 19, 2018 at 13:06
  • No, doesn't work for i==2. Commented Jan 19, 2018 at 14:00

1 Answer 1

1

After the inner loop (which uses "k"), there is a "itemp = indRankcount;" line. I guess this cause the issue.

On the first run the "itemp" is 0 so the inner loop step in, but on the second run this value more or equal with the "indRankcount", because you call the code before. What values are stored in "iRankcount0", "iRankcount1" and "iRankcount2"? Try to print the "itemp" and "indRankcount" values before the 2. loop.

Updated, try this before the k loop, it will show why the k not starts on the 2. execution.

Console.log(i + "loop:: " + itemp + " val (k first val), " + " indRankcount " + val (k end val));
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your response. For i=0, itemp=0, indRankcount = 7. Then I need to move on to my next group to validate, so I move indRankcount to itemp, I also have indRankcount1 = 11 and move that to indRankcount. For i=1, itemp=7, which is start of k and indRankcount=11. This is one case, but my group can vary.
Do the "iRankcount0", "iRankcount1" and "iRankcount2" values contains increasing numbers ? Because if ( iRankcount0 > iRankcount1 > iRankcount2) than the inner loop doesn't execute, just on the first time as you got it before. Try to console.log the "i" and the "k" at the start of the loop. Is it return with false after the first inner loop execution?
Actually its iRankcount0 < iRankcount1 < iRankcount2.
Try to console.log the values on every "i" loop. Console.log(i + "loop:: " + itemp + " val (k first val), " + " indRankcount " + val (k end val)); Set it before the k loop and it will show why the k not starts on the 2 execution.
Thank you, Its very helpful. I'll keep it posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.