Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 23rd, 2005, 03:00 PM
Authorized User
 
Join Date: Aug 2005
Location: , , .
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Default javascript: help needed

hello
i've just registered with this forum and this iz my 1st post
actually i m stucked in a problem in JavaScript ... n i hope that someone will help me

i want 2 develop a program in javascript ... that generates 100 random numbers from 0 to 99 ... n all those random numbers are to be stored in a 100 length array ... but the actual thing iz that all the rendomly generated numbers should be unique and there should be no duplication ...
at last there should be a complete array of 100 elements having 0 to 99 numbers randomly stored in it ... i mean any number on any index

i hope i'll get my answer as soon as possible ... bcoz there r so many experts in this form and i'll learn so much here

waiting 4 a good reply
  #2 (permalink)  
Old August 24th, 2005, 06:55 AM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Kanoorani!!
Offcourse you can do this,
hope this will help you
******test.html****
<form name="myform">
<textarea name="mt" ></textarea>
<input type=button onclick="setVal()">
</form>
<script>
var
flag=true;
var myaray
myarray=new Array()
var myarrayString
loopcounter=0
function setVal()
{
str=""
newstr="/"
var currval;
var flag
flag=true;

 while(flag)
     {
      currval=rnd(1,100)
      str="/"+ currval+"/"

      if(newstr.indexOf(str)==-1)
       {
     //alert(loopcounter)
       myarray[loopcounter]=currval;
       loopcounter++;
       if(loopcounter==100)
        {
        flag=false
         }
         newstr=newstr+currval+"/"
       }

       myarrayString=myarray.join("~");

     }
document.myform.mt.value=myarrayString

myarray.sort(compareNum)
for(i=0;i<myarray.length;i++)
{
alert(myarray[i]);
}

}
function setRnd()
{
setVal()
}


function rnd(a,b) {
    return a + Math.floor(Math.random()*(b-a+1));
}

function compareNum(a,b)
{
return a-b
}
</script>

****8test.html******end*******

please comment following lines,this is just to show sort function

myarray.sort(compareNum)
for(i=0;i<myarray.length;i++)
{
alert(myarray[i]);
}



Cheers :)

vinod
  #3 (permalink)  
Old August 24th, 2005, 07:25 AM
Authorized User
 
Join Date: Aug 2005
Location: , , .
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello
thankx alot my friend ... it worked really very nice ... i'll b contacting u in future if i face n.e problem ... thankx once more



Similar Threads
Thread Thread Starter Forum Replies Last Post
stop current javascript function using javascript sakthi Javascript 3 June 2nd, 2008 03:30 PM
HELP NEEDED Yasho VB.NET 2002/2003 Basics 1 January 29th, 2007 08:13 AM
HELP NEEDED G.Mohanraj Other Programming Languages 1 January 23rd, 2006 01:22 PM
Help needed please please PhillipMorris Access ASP 4 September 23rd, 2003 11:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.