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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I need help with my newArray script

    <script>
    var quotes = new Array()
    quotes[0] = "haha";
    quotes[1] = "lala";
    quotes[2] = "woowoo";
    var ramath = math.round(math.random()*3)
    var quote = quotes[ramath]
    document.write(quote)
    </script>

    I just wrote stuff down for the quotes just to see if it'd work. I haven't been able to get it to work so far. What's wrong with it?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just some minor mistakes:

    <script>
    var quotes = new Array()
    quotes[0] = "haha";
    quotes[1] = "lala";
    quotes[2] = "woowoo";
    var ramath = Math.round(Math.random() * 2)
    var quote = quotes[ramath]
    document.write(quote)
    </script>

    I changed the multiplication factor to 2 because that will give you random numbers between 0 and 2 - exactly the range of your array.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    United Kingdom Confused: Often
    Posts
    859
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you want to be adding more quotes and not changing that number every time you could use this:

    <script>
    var quotes = new Array()
    quotes[0] = "haha";
    quotes[1] = "lala";
    quotes[2] = "woowoo";
    var ramath = Math.round(Math.random() * quotes.length)
    var quote = quotes[ramath]
    document.write(quote)
    </script>

    happy coding
    redhead

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    New York, USA
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by redhead
    if you want to be adding more quotes and not changing that number every time you could use this:
    that's a better way to go in my opinion but you would need to use:
    Math.floor(Math.random() * quotes.length)

  • #5
    New to the CF scene
    Join Date
    Aug 2002
    Location
    Weldon, California
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by mordred

    <script>
    var quotes = new Array()
    quotes[0] = "haha";
    quotes[1] = "lala";
    quotes[2] = "woowoo";
    var ramath = Math.round(Math.random() * 2)
    var quote = quotes[ramath]
    document.write(quote)
    </script>
    Okay, what would I need to do to this script to make it display sequentially (in order) and not randomly?

    Thank you.
    Sharon Montero
    iWomen Online Network

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Sharon Montero


    Okay, what would I need to do to this script to make it display sequentially (in order) and not randomly?

    Thank you.
    Ok boys, I'll take the tough ones here..

    Code:
    for (var i=0; i< quotes.length; i++) {
       document.write (quotes[i];
    }

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Sharon Montero [/i]
    Okay, what would I need to do to this script to make it display sequentially (in order) and not randomly?
    Take out the randomizing part...

    Seriously, RadarBobs solution will print the values of each array field to the document (well, if you add the missing last parentheses). I'll have a slight notion that you could mean something different with "display sequentially in order", perhaps you have to elaborate more on this part.

    Ah yes, there's a different way to get all quotes printed at once:

    Code:
    document.write(quotes.join("<br />"));


  •  

    Posting Permissions

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