For Loops

Discussion in 'Javascript' started by Gregc., Aug 30, 2006.

  1. Gregc.

    Gregc. Guest

    Hi

    I am not sure you guys can help me, but here it goes. am having
    trouble understanding for loops. If I have a loop that says:


    var coffee = new Array();
    coffee ["mixedblend"] = 5.50;
    for (c in coffee)

    {code goes here}

    That for saying while there is a c in coffee, then conduct the code.
    Is that a correct interpretation?

    Greg
     
    Gregc., Aug 30, 2006
    #1
    1. Advertising

  2. Gregc.

    Randy Webb Guest

    Gregc. said the following on 8/29/2006 8:27 PM:
    > Hi
    >
    > I am not sure you guys can help me, but here it goes. am having
    > trouble understanding for loops. If I have a loop that says:
    >
    >
    > var coffee = new Array();
    > coffee ["mixedblend"] = 5.50;
    > for (c in coffee)
    >
    > {code goes here}
    >
    > That for saying while there is a c in coffee, then conduct the code.
    > Is that a correct interpretation?


    Yes.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Aug 30, 2006
    #2
    1. Advertising

  3. JRS: In article <>,
    dated Tue, 29 Aug 2006 17:27:07 remote, seen in
    news:comp.lang.javascript, Gregc. <> posted :

    >I am not sure you guys can help me, but here it goes. am having
    >trouble understanding for loops. If I have a loop that says:
    >
    > var coffee = new Array();
    > coffee ["mixedblend"] = 5.50;
    > for (c in coffee)
    >
    >{code goes here}
    >
    >That for saying while there is a c in coffee, then conduct the code.
    >Is that a correct interpretation?


    No.

    Consider and try the following

    var coffee = new Array();
    coffee ["mixedblend"] = 5.50;
    coffee ["grots"] = 1.50;
    for (c in coffee) alert (c + " costs " + coffee[c])

    IMHO, it is generally better to use Array only for numeric indices, and
    the above could start var coffee = {} // empty Object.

    The ordering apparent in coffee by for (c in coffee) is undefined.

    You should use var c; and, as that is essentially an indexing operation,
    I'd use J instead of c.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Aug 31, 2006
    #3
  4. Gregc.

    Gregc. Guest

    Dr John Stockton wrote:
    > Consider and try the following
    >
    > var coffee = new Array();
    > coffee ["mixedblend"] = 5.50;
    > coffee ["grots"] = 1.50;
    > for (c in coffee) alert (c + " costs " + coffee[c])
    >
    > IMHO, it is generally better to use Array only for numeric indices, and
    > the above could start var coffee = {} // empty Object.
    >
    > The ordering apparent in coffee by for (c in coffee) is undefined.
    >
    > You should use var c; and, as that is essentially an indexing operation,
    > I'd use J instead of c.
    >

    Hi

    what does IMHO mean?

    Greg
     
    Gregc., Aug 31, 2006
    #4
  5. Gregc.

    Gregc. Guest

    Gregc., Sep 1, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eismaus4

    to many FOR loops?

    eismaus4, Apr 27, 2004, in forum: VHDL
    Replies:
    1
    Views:
    698
  2. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl
    Replies:
    1
    Views:
    5,172
    Big and Blue
    Jan 30, 2005
  3. Multiple For Loops?

    , Feb 22, 2006, in forum: VHDL
    Replies:
    1
    Views:
    487
    john Doef
    Feb 22, 2006
  4. =?Utf-8?B?Um9iIE8nQ29w?=

    RedirectFromLoginPage and loops

    =?Utf-8?B?Um9iIE8nQ29w?=, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    421
    =?Utf-8?B?Um9iIE8nQ29w?=
    Oct 25, 2004
  5. Me
    Replies:
    2
    Views:
    260
Loading...

Share This Page