-2

I just started learnning javascript loops, and I created a simple first loop which doesn't work and I can't figure out why,

please try to keep it simple for now as I'm just a beginner, try to correct it by doing simple things if possible and if not just tell me and I will study some more about it,

the code is supposed to send an alert if the value of a input text has been left empty

    <form name="myForm" runat="server" onsubmit="return myFunction()">
<div>
First name<input type="text" id="firstname" name="fname" />
<input type="submit" value="Submit" />
</div>
</form>
<script>
    function myFunction() {
        var x = document.forms["myForm"]["fname"].value;
        var i = x.length;
        for (var i = x.length; i = 0; i++) {
            alert("asfasfas");
        }
    }


</script>

What did I do wrong?

4 Answers 4

3

Your loop can be broken down into these instructions:

  • Set i to be x.length
  • If i = 0 is true... uh-oh. That's an assignment, there, and the assignment is a falsy value. The loop ends there.

I suspect you may have meant this:

for( i=0; i<x.length; i++)
Sign up to request clarification or add additional context in comments.

Comments

0

You're starting at with i = x.length, and testing to see when i=0. Traditionally, you'd start at 0 and test to when i==x.length. You've also used = (assignment) instead of == (comparison). You really want <x.length, because that middle term is really "do while this term is true"

Try this modification::

for (var i = 0; i < x.length; i++) {
        alert("asfasfas");
    }

2 Comments

This code doesn't work, when I press submit the alert won't trigger
see the other answer regarding the order of loading. The function may not be in the symbol table if you don't load the script first. This is emulated in this fiddle: jsfiddle.net/5jryx/1
0

For loop has three major parts:

for ([1. initiation];[2.condition];[3.step]) {
...
}
  1. Initiation : Executed once before the loop starts;
  2. Condition: Executed every time BEFORE the loop body and exits the loop if it is false
  3. Step: Executed each time AFTER the loop

In your example, the condition always returns false, because i=0 returns 0 and it is treated as false in javascript.

What you probably wanted to do is this:

for (var i=0; i<x.length; i++) {
   alert("Found letter:"+x.charAt(i));
}

Alternatively, you could do

for (var i=x.length-1; i>=0; i--) {
   alert("Found letter:"+x.charAt(i));
}

It iterates the same number of times through the same values of i, but backwards.

I recommend to read more on the for loops here: http://www.w3schools.com/js/js_loop_for.asp

2 Comments

Can you please explain me the last part +x.CharAt(i)
JavaScript is case-sensitive. Functions start with lower case. So, it is x.charAt(i). Since your x contains the string user typed into the input field, x.charAt(index) will take a character at the provided index. So, the loop will show the string letter by letter. Please, upvote if you find it useful.
0

Ok, a couple of things.

  1. You are using a for loop when you'd be better with an if statement (if length is = 0, then throw the alert).
  2. You have no action. Not sure if that really matters, but I added one for completion's sake.

<form name="myForm" action="javascript:void(0);" onsubmit="return myFunction()">
 <div>
  First name<input type="text" id="firstname" name="fname" />
  <input type="submit" value="Submit" />
 </div>
</form>
<script>
    function myFunction() {
        var x = document.forms["myForm"]["fname"].value;
        var i = x.length;
        if (i == 0) {
            alert("asfasfas");
        }
    }
</script>

1 Comment

The reason I'm using a for loop instead of if is because when I have a long form with a lot of vars for example 7 I need to create a code for each option that can happen, for example all 7 are empty than 1-6 are than 2-7 than 3-7+1, etc... I wrote about it here stackoverflow.com/questions/20637995/javascript-multiple-events

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.