Solved

handling random values

Posted on 2012-03-19
4
167 Views
Last Modified: 2012-03-19
Hi,

Please see my code below:

I only want to generate random values 0-2 however it goes up to 10.
Can someone please advise how I should adjust my random function.

thanks


var values = new Array();

values[0]="hello1";
values[1]="hello2";
values[2]="hello3";
values[3]="hello4";

while (confirm("again?"))
{
      document.write("<pre>");
      document.writeln(values[Math.floor(Math.random()*10)]);
      document.write("</pre>");
}
0
Comment
Question by:jecommera
[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 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 37738269
Change Math.random()*10 with Math.random()*2
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37738277
while (confirm("again?"))
{
      document.write("<pre>");
      document.writeln(values[Math.floor(Math.random()*2)]);
      document.write("</pre>");
}

Open in new window



Cd&
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37738285
Sorry Cluskitt, I didn't see your post


Cd&
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37738295
No problems, you posted within seconds of me, it happens all the time. As long as there is a solution, that's all that matters.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Worldmap 1 28
Html split(text) Part2 6 20
Duplicate Elements with JS / jQuery 3 24
Open grouped links on a page in new tabs via jquery 5 41
This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

740 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