Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    a for loop/array question

    I feel I understand arrays pretty well, at least the javascript semantics of them. I also follow the structure of for-loops adequately. But I'm really stumped by the following piece of code:


    var abc = new Array(0,1,1,1,2,3,4,0,6,-1,-1,-1,-2,-3,-4,0,-6);
    for (i=0; i < abc.length; i++)
    var C=Math.round(Math.random()*[i]);
    howbend = abc[C];


    Here are my main questions (aside from the overall "what does it do"):

    Isn't C declared inside the for-loop, making it "destruct" or die away to nothing upon exiting the for-loop, since it's local to the loop? If so, the last line should send an error message of an undefined object, or at least that C from abc[C] should not have a predictable value (and we're not blaming the random number generator here).

    What does [i] mean from the statement inside the for-loop? I feel like it's related to the abc array being used as the conditions for the for loop, but it's not specified, and even if it was, that C is still bugging me.

    If it does loop through the entire deal, does each value seen by C somehow have an affect on the final value of howbend, or just that last value computed during the for-loop. To state it another way, from what I see, C should be assigned a different value for EACH ITERATION of the for-loop. Yet it goes through it almost twenty times. Is there some kind of summation taking place, or is the value from the last iteration the only one recorded in C as the loop is terminated?

    If you can't answer the questions themselves (and I wuoldn't blame you), are there any references to where I could research this further? Please Advise. Thanks in advance.
    If at first you don't succeed, spend more time online researching javascript!
    Beck

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    javascript has no "block level", so C remains defined once that loop has ended. as for that [i], i don't know what that is; that should be throwing an error, as far as i know. it certainly looks like it's supposed to have to do with the array

    c is being assigned a new value on every iteration. the new value replaces the old value.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know what it's for, I'd guess it's generating random numbers.

    This will show you what C and howbend are with each pass through the loop. Because I made them global, when the loop is complete, they'll take the value of the last pass through the loop.

    <html>
    <head>
    <title></title>
    <script>
    C = ''
    howbend = ''
    array2 = new Array()
    array3 = new Array()
    var abc = new Array(0,1,1,1,2,3,4,0,6,-1,-1,-1,-2,-3,-4,0,-6);
    for (i=0; i < abc.length; i++) {
    var C=Math.round(Math.random()*[i]);
    howbend = abc[C];
    array2[array2.length] = C
    array3[array3.length] = howbend
    }
    alert('C = \n\n'+array2.join('\n'))
    alert('howbend = \n\n'+array3.join('\n'))
    alert('C = '+C)
    alert('howbend = '+howbend)
    </script>
    </head>
    <body>
    </body>
    </html>

    i is set to 0 when the loop starts then incremented by 1 with each pass. When i equals abc.length the loop stops.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    John is right. I didn't notice that [i]. Now I'd like to know why [i] isn't throwing an error.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just thinking, it should mulitply i by Math.random if it's written like this

    var C=Math.round(Math.random()*i);

    Maybe it's just unrequired brackets that still function, something like this

    if ((C=='')) {
    alert('here')
    }

  • #6
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    yeah, that would work. the brackets would make it an array literal. which would make absolutely no difference in this particular case.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    the overall "what does it do"

    Best to take it row by row here:
    Code:
      var abc = new Array(0,1,1,1,2,3,4,0,6,-1,-1,-1,-2,-3,-4,0,-6); 
    // Bet you understand this.
    //Creates an array with the given values as members.
      for (i=0; i < abc.length; i++)
    //Bet you understand this as well
    //This loop construct is the basic for loop as given in as example in book etc.
        var C=Math.round(Math.random()*[i]);
    // Don't understand why anyone would do this -
    // it's in effect only the last value of i that will be used,
    // as C is overwritten each turn through the loop;
    // in other words, the index of the last element in the array is the only value of i that is used.
    // The brackets around i have no effect whatsoever due to the fact that
    // JavaScript does silent type conversion when needed if possible, 
    // and a one-element array can be type-converted to it's content type.
      howbend = abc[C];
    // In the end, howbend is assigned the value of a random element in the abc array.
    // This could have been done far easier, so I guess the author doesn't really understand what he/she is doing.
    If I were to make a guess, this code is supposed to create a biased random value from a set - if so, there are better and easier ways of doing it, here an example:

    Code:
      
      var howbend=(Math.round(Math.random())||-1)*[0,1,1,1,2,3,4,0,6][Math.round(Math.random()*8)];
    Last edited by liorean; 03-03-2003 at 11:45 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks

    thanks to all who replied and conversed over this. Made me feel warm inside

    I have the original script and author's email. I'll ask him what he's expecting to come out of this. Maybe I'll post when I get an answer, decide it's worth sharing, and can still find this posting in the forum.

    liorean - thanks for that last bit of more efficient looking code, I'll use that. (Showoff! )
    If at first you don't succeed, spend more time online researching javascript!
    Beck

  • #9
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    so much for that idea...

    It seems the writer of the script has an email server past quota, so that scratches that. Now I guess we'll never know
    If at first you don't succeed, spend more time online researching javascript!
    Beck


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •