www.webdeveloper.com
Results 1 to 5 of 5

Thread: large array with vocab words

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    large array with vocab words

    can anyone help me diagnose this file? nothing is outputted.

    <html>
    <head>
    </head>
    <script type="text/javascript">
    <!--
    var r_text = new Array ();
    r_text[0]="nusnu";
    r_text[1]="bhbtrh";
    .
    .
    .
    .
    r_text[5000]="hsm7i";
    phraseCnt = r_text.length;
    var i = Math.floor(phraseCnt*Math.random())
    document.write(r_text[i]);
    //-->
    </script>


    </html>

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    Code:
    document.write(i);
    That should show you why it isn't working, and the solution is to use this instead.
    Code:
    document.write(r_text[Math.floor(r_text.length*Math.random())]);
    EDIT: I totally missed the Math.floor() when I read that the first time. It works fine for me like that, provided that you have all the values filled in.

    As a side note, it would be easier for you to declare your array using JSON.
    Code:
    var r_text = ["no. 1", "no. 2", "no. 3" /* etc. */];
    Last edited by Declan1991; 08-09-2010 at 09:30 PM.

  3. #3
    Join Date
    Jun 2007
    Posts
    667
    Provided that there are no gaps in the subscripting, I don't see anything wrong and it does run.

    Did you really write r_text[n]= 'word' 5000 times?

  4. #4
    Join Date
    Aug 2010
    Posts
    3
    i actually have vocab words in there. so it's a long string, not just one word.

  5. #5
    Join Date
    Aug 2010
    Posts
    3
    fixed. i had a stray quotation mark at the 319th array.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles