...

View Full Version : I need help with my newArray script



Porygonix
08-03-2002, 04:30 PM
<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?

mordred
08-03-2002, 05:12 PM
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.

redhead
08-03-2002, 05:15 PM
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 :thumbsup:

boywonder
08-03-2002, 07:14 PM
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)

Sharon Montero
08-04-2002, 01:03 AM
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.

RadarBob
08-04-2002, 04:31 AM
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..



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

mordred
08-04-2002, 05:00 AM
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:



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum