Avatar of jecommera
jecommera
Flag for United Kingdom of Great Britain and Northern Ireland asked on

handling random values

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>");
}
JavaScript

Avatar of undefined
Last Comment
Cluskitt

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Cluskitt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
COBOLdinosaur

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

Open in new window



Cd&
COBOLdinosaur

Sorry Cluskitt, I didn't see your post


Cd&
Cluskitt

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck