Solved

handling random values

Posted on 2012-03-19
4
162 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now