Solved

handling random values

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add Clickable Links to Google Map Markers 3 27
How can i get the length? 7 31
More SharePoint App with Angular and Bootstrap 17 46
Checking data with javascript 3 19
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 …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

805 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