Solved

How to choose RANDOM between 4 specified document.write ?

Posted on 2009-07-10
4
516 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

624 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