?
Solved

handling random values

Posted on 2012-03-19
4
Medium Priority
?
173 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

719 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