-1

Just trying to figure out my error of "strange loop". I receive this error in JSLint and the loop just keeps going over and over again whereas it needs to stop.

<script type="text/javascript">
function checkForm(theForm) {
    "use strict";
    /*global alert*/
    var formValid = true,
        elementCount = 0;
    while (elementCount <= theForm.length) {
        if (theForm.elements[elementCount].type === "text") {
            if (theForm.elements[elementCount].value() === "") {
                alert("Please complete all the form elements");
                theForm.elements[elementCount].focus();
                formValid = false;
                break;
            }
        }
        return formValid;
    }

} </script>

Any help or guidance is appreciated, please explain or show me as I am a novice.

rworthy7
  • 23
  • 4
  • 2
    To make it easier for answerers, or others with similar problems, please [edit] to add a more specific problem statement — "it doesn't work" can be assumed, but *how* does it not work? What error message or incorrect behavior is characteristic? – Nathan Tuggy Sep 19 '15 at 02:26
  • What exactly is the value of `theForm`? Where are you changing the value of `elementCount`? How can the loop run forever if there is a `return formValid;` statement that will definitely stop the loop? There should be at most one iteration. – Felix Kling Sep 19 '15 at 02:33
  • You're not incrementing elementCount within your loop ... so it's forever checking the same element over and over again ... `elementCount = elementCount + 1` – Ragdata Sep 19 '15 at 02:38
  • @Ragdata So you're referring to the 6th line and that it should be changed, correct? – rworthy7 Sep 19 '15 at 02:43
  • No ... just a tick and I'll put it into an answer for you – Ragdata Sep 19 '15 at 02:44
  • That loop CANT run forever, it has a return at the end, so it'd run once. – JSelser Sep 19 '15 at 02:54
  • You could debug your own code. Do you know how to use the debugger? If not, please stop everything you are doing and learn. You could place a breakpoint on the `while` line, for example. When it stops there, examine the value of `elementCount` and see if you see anything suspicious. Or, you could put a `console.log` statement inside the `while` and print out variables of interest to the console. It's going to be very hard to become a great programmer if you don't learn the basics of how to find your own bugs. After all, you cannot realistically post every problem you run into to SO. –  Sep 19 '15 at 04:05
  • @torazaburo The purpose of this assignment for me was actually to debug. I actually debugged everything and this is my last issue I'm running into. I will insert the break at the WHILE line and see where that takes me.Thanks for your help! – rworthy7 Sep 19 '15 at 13:21

2 Answers2

2

As others have said, you need to increment that counter. However it might be clearer with a for loop, which often looks like this:

for (var elementCount = 0; elementCount <= theForm.length; elementCount++) {
    // do stuff
}

It's clearer because the incrementing happens right at the top of the for expression.

Community
  • 1
  • 1
tragle
  • 121
  • 7
1

You're not incrementing elementCount within your loop. Try this:

<script type="text/javascript">
    function checkForm(theForm) {
        "use strict";
        /*global alert*/
        var formValid = true,
        elementCount = 0;
        while (elementCount < theForm.length) {
            if (theForm.elements[elementCount].type === "text") {
                if (theForm.elements[elementCount].value === "") {
                    alert("Please complete all the form elements");
                    theForm.elements[elementCount].focus();
                    formValid = false;
                    break;
                }
            }
            elementCount++;
        }
        return formValid;
    }
</script>

Notice that I also moved your return statement outside your loop.

Ragdata
  • 1,156
  • 7
  • 16
  • As a result of that code, Im now throwing the error "unexpected ++" at the new line you added in. Just FYI I'm Using Brackets. – rworthy7 Sep 19 '15 at 02:53
  • Oops ... check the edit (forgot which language I was using) – Ragdata Sep 19 '15 at 02:57
  • @Ragdata I think your original increment was correct - the increment operator (`++`) is totally valid JavaScript. I wonder why it threw that error... – Maximillian Laumeister Sep 19 '15 at 03:03
  • Even with that edit the error is still thrown via JSLint. What am I missing here? – rworthy7 Sep 19 '15 at 03:03
  • Something to be said for using jsfiddle too ... it found another error for me. Note then when checking the value, I've removed your brackets. ( error - value is not a function ) – Ragdata Sep 19 '15 at 03:03
  • `elementCount = elementCount++` is quite a strange construct. Normally one would just say `elementCount++`. As you have it, what it seems to be saying is, "assign `elementCount` to `elementCount`, then increment `elementCount`. Or, you could say `elementCount += 1;`, or even `elementCount = elementCount + 1;`. –  Sep 19 '15 at 04:07