Solved

How to choose RANDOM between 4 specified document.write ?

Posted on 2009-07-10
4
513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
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)

734 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