Solved

How to choose RANDOM between 4 specified document.write ?

Posted on 2009-07-10
4
506 Views
Last Modified: 2013-11-11
Hi,
I want the script to choose random between 4 specific choices.

Using this code it does that, but somehow it NEVER goes to pick choice number 2.  Or, it shows version 1, 3 and 4 - but never number 2. It in stead shows nothing every once in a while.

Where's the error?

<script type="text/javascript">
var random_number = Math.random();
if (random_number < 0.25){
document.write("Hello 1!");
}
else if (random_number >0.25  <0.5){
document.write("Hello 2!");
}
else if (random_number >0.5  <0.75){
document.write("Hello 3!");
}
else if(random_number >0.75  <1){
document.write("Hello 4!");
}
</script>
<script type="text/javascript">
var random_number = Math.random();
if (random_number < 0.25){
document.write("Hello 1!");
}
else if (random_number >0.25  <0.5){
document.write("Hello 2!");
}
else if (random_number >0.5  <0.75){
document.write("Hello 3!");
}
else if(random_number >0.75  <1){
document.write("Hello 4!");
}
</script>

Open in new window

0
Comment
Question by:Maxrespons
  • 2
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 125 total points
ID: 24822069
var pick = Math.floor( Math.random() * 4 )

Result will be an integer from 0 to 3, which could/should be used to index an array:

var message = [ 'Hello 1', 'Hello 2', 'Hello 3', 'Hello 4' ]

document.write( message[ pick ] );
0
 

Author Comment

by:Maxrespons
ID: 24822210
Thank you!
I used my code but with your Math.floor( Math.random() * 4 )

and made these changes:
if (random_number == 0){

up to

if (random_number == 3){

That made it all work perfect ;-)
0
 

Author Closing Comment

by:Maxrespons
ID: 31602016
Thanks for helping! - Combining your code and my code made it work like I needed. :-)
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24827991
Excellent.  Glad to be able to help.

Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question