Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Looping Statements in JavaScript - 09

Download

Right click “Save as…”

Looping (or rather, iteration) statements allow your application to individually access each item in an array or other collection of object properties. In this lesson we demonstrate four different looping statements pointing out the nuanced differences between each: the while, do ... while, for, and for ... in.

Tag:

Follow the Discussion

  • Ron SwankRon Swank

    Bob you do an absolutely great job in presenting this information in an easy and understandable way that is not boring!

    It helped me clarify the loops by adding a playList[2] undefined when defining the titles, and numbered the titles.
    playlist[0] = "0- One for the team"
    playlist[1] = "1- The Joy Formidable"
    playlist[2]
    playlist[3] = "3- Young the Giant"
    playlist[4] = "4- Washed Out"

    Then each for loop examples showed what the loop was seeing, especially the “for...in” loop.

  • Re-opening this closed thread ...

  • MattMatt

    Hi.
    Im working with php and to mee the codes are understadable at first.
    But, im getting interested in the next videos, because i want to know how to attach javascript with PHP lines, also attach Javascript with mysql querys and functions.

    Also interested how to create Javascript pop down windows like suggestion bars.

    Then how to make the jacascript execute the most server time to the user computer :)

    BUt theese videos are great :D watch them day by day.

  • Ilya SitnikovIlya Sitnikov

    Great videos!

    I had a question, though...

    For loop was showing undefined for me instead of displaying band names.

    The rest of loops worked fine - what am I doing wrong?

    Mine looks like this:

    for (var i = 0; i < playlist.length; i++);
    {
    document.write("<br/>Now Playing: " + playlist[i]);
    }

  • Ilya SitnikovIlya Sitnikov

    Great videos!

    I had a question, though...

    For loop was showing undefined for me instead of displaying band names.

    The rest of loops worked fine - what am I doing wrong?

    Mine looks like this:

    for (var i = 0; i < playlist.length; i++);
    {
    document.write("<br/>Now Playing: " + playlist[i]);
    }

  • You have a ; at the end of your 'for' statement that ends it right there.  It never executes the block of code beneath it.  Remove the ; and it should work.  Good luck!

     

  • Hi Bob!!! I purchased your personal collection (lifetime!!) Smiley and also skilling up on C#, HTML5,CSS3, JavaScript & JQuery. I am SICK OF YOUR VOICE!!!!

    Seriously though;

    When defining variable in C# or JavaScript we have: Strings, Integers or Bolean as options

    If it's integers we normally start with 0 and change it:

    var myVar = 0

    if (some code) {} else, etc, etc...

    ----

    But I hit a wall when I either define the initial value as a NEGATIVE integer, or, if it's part of a looping code block becomes a negative integer.

    Is this illegal? Is there a way around it ? Or do I just stay with Positive integers as variables?

     

    Cheese from Australia!

    Ippy

     

  • @Ippy: Check this out:

    http://jsfiddle.net/tugbx/

    I think it does what you asked for ... initializing a variable to a negative number and even doing a for loop with negatives.

    Does that help?

  • Bingo!

    Thank you,

    The learning curve will never end!

    cheeze

    Ippy

  • LarsLars

    Great series!! On Q though (regarding the for loop):

    The example you give is:

    for (var i = 0; i < playlist.length; i++)
    {
    document.write("<br />Now Playing: " + playlist[i]);
    }

    My Q is: Why will incrementing the iterator take place AFTER the loop has been performed and not before? I mean why is the incrementing part not read before the loop part of the code has had a chance to run?

  • @Lars: I realize this may not be a satisfying answer, but to answer why?  That's just the way it works.  The iterator does not increment until after the code block has executed once.  If you want the iterator to start at 1 instead of 0, then do this instead:

    for (var i = 1; i < playlist.length; i++)

    Here's a URL with more information than you would ever want to know about loops, performance, variations, etc.:

    http://james.padolsey.com/javascript/looping-in-javascript/

    You might consider the 'while' or 'do..while' loop instead of the 'for' in some cases.

  • LarsLars

    Hi Bob, Thank you for your answer! Well I guess that is as good an answer as any - just a fact to be remembered - not necessarily to be fully understood. And thanks for the link!

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.