im a total NEWB i need a little help understanding this script..specific instances are noted.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nightsaber

    im a total NEWB i need a little help understanding this script..specific instances are noted.

    <script language="JavaS cript">
    <!-- hide me

    var the_number = prompt("how many words (3-5 is good)?", "4");
    var the_string = "";
    var a_word;

    for (loop = 0; loop < the_number; loop++)
    {
    line_number = loop + 1;
    a_word = prompt("what's word " + line_number + "?","");
    the_string = "word " + line_number + ": " + a_word + "<br>" +
    the_string;
    }
    var new_window =
    window.open("re verse.html","re verse","height= 400,width=400") ;
    new_window.docu ment.writeln("< h1>In reverse</h1>");
    new_window.docu ment.writeln(th e_string);

    // show me -->
    </script>
    ok heres what i DONT get and it kinda screws upo the whole thing for
    me.[color=blue]
    >for (loop = 0; loop < the_number; loop++) ok loop++ means loop = loop +1 so loop is now 1 <0+1=1>
    >{
    > line_number = loop + 1; at this point it ADDS 1 to loop which is as i said 1 making "line_numbe r" = 2 <1+1>[/color]
    yet thats NOT how it works. it puts the value as 1. take out the +1
    part and it makes the line number 0 ...i just dont get this at all. i
    started on this java tutorial thing today at 7am..... its now 2 45 am
    I understand the majority of it up to here....
    of course[color=blue]
    >the_string = "word " + line_number + ": " + a_word + "<br>" + the_string; kinda throws me it seems like its saying the _string = "word 1 : apple"+"word 1 : apple"[/color]
    but i suppose im being to literal. i am guessing this adds the
    afformentioned value to itself so the _string DOESNT= "word 1 :
    apple"+"word 1 : apple"it just = "word 1 : apple" and whatever the
    next word is will be added to that so it will be the_string="wor d 2
    :orange, word 1 :apple"
    any help is appreciated. i like to understand not only HOW but WHY?!
    cause i see what it does i just dont know WHY and that bugs the heck
    outta me.


  • Michael Stemper

    #2
    Re: im a total NEWB i need a little help understanding this script..specifi c instances are noted.

    In article <0728hvkhpr0e3e n2frcitlppkp6bs tkd3g@4ax.com>, nightsaber <nightsaber@hot mail.com> writes:
    [color=blue]
    >for (loop = 0; loop < the_number; loop++)
    >{
    > line_number = loop + 1;
    > a_word = prompt("what's word " + line_number + "?","");
    > the_string = "word " + line_number + ": " + a_word + "<br>" + the_string;
    >}[/color]
    [color=blue]
    >ok heres what i DONT get and it kinda screws upo the whole thing for
    >me.[color=green]
    >>for (loop = 0; loop < the_number; loop++) ok loop++ means loop = loop +1 so loop is now 1 <0+1=1>[/color][/color]

    This is what's called a "for loop". The three things in the for statement
    are:
    initializtion: loop = 0;
    test: loop < the_number;
    increment: loop++;

    The way that a for loop works is:
    1. Execute the initialization:
    loop = 0;
    2. Execute the contents of the loop
    {
    line_number = loop + 1;
    a_word = prompt("what's word " + line_number + "?","");
    the_string = "word " + line_number + ": " + a_word + "<br>" + the_string;
    }
    (Note that, at this point, the value of "loop" is zero.)
    3. Execute the increment:
    loop++;
    (Only at this point does "loop" get set to one.)
    4. Execute the test:
    loop < the_number;
    If the test is satisfied (IOW, if "loop" is less than "the_number "),
    you go back to step 2, and the contents of the loop are again
    executed.
    [color=blue][color=green]
    >> line_number = loop + 1; at this point it ADDS 1 to loop which is as i said 1 making "line_numbe r" = 2 <1+1>[/color][/color]

    But, that addition doesn't happen until after the contents of the loop
    have been executed.

    --
    Michael F. Stemper
    #include <Standard_Discl aimer>
    Give a man a fish, and you feed him for a day. Teach him how to fish,
    and you can sell him equipment.

    Comment

    Working...