Solved

javascript array without duplicates

Posted on 2012-03-22
17
331 Views
Last Modified: 2012-03-26
How can I create a javascript array with a variable quantity without any duplicates?

function randomFromInterval(from, to, qty) {

    var arr = [];

    for (var i=0; i <= qty; i++) {	

        arr[i] = Math.floor(Math.random() * (to - from + 1) + from);	

    }

    return arr;

}

Open in new window


I'm so confused by this. I've locked up my browser with infinite loops so many times. thanks so much for helping!
0
Comment
Question by:hibbsusan
  • 8
  • 5
  • 4
17 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37755042
Thousands on the web

This one seems quite elegant

http://stackoverflow.com/a/2380349/295783
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37755061
Try this :

    Array.prototype.inArray = function(v) {
        for(var i=0;i<this.length;i++) if(this[i] === v) return true;
        return false;
    }

    function randomFromInterval(from, to, qty) {
    
        var arr = [];
    
        while(arr.length<=qty) {    
            var r = Math.floor(Math.random() * (to - from + 1) + from);
            if(!arr.inArray(r)) arr.push(r);    
    
        }
    
        return arr;

    }
 

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37755076
You mean indexOf since inArray is jQuery
0
 

Author Comment

by:hibbsusan
ID: 37755085
leakim971,

That seems very simple and really great! Thanks!

How does prototype work?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37755124
0
 

Author Comment

by:hibbsusan
ID: 37755138
that prototype thing kind of broke the rest of my code though. now the page won't load.

can i use it in such a way that it won't affect any other code?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37755192
Please provide a link to see your page
test page here : http://jsfiddle.net/VwFDv/
0
 

Author Comment

by:hibbsusan
ID: 37755216
This is with the randomFromInterval() function at the top of this page that I started out with

Here's where I installed the function you've supplied
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 82

Expert Comment

by:leakim971
ID: 37755271
you should replace :  while(arr.length<=qty) {  
by :  while(arr.length<qty) {  

but it will not fix your code.
I think the problem come from the fading process
0
 

Author Comment

by:hibbsusan
ID: 37758168
so there's not really a good way to do this without using prototype, in your opinion?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 37758349
Prototype is just one method to add a method to an object
You can use the same function without using the prototype.

Here is one using indexOf

function randomFromInterval(from, to, qty) {
  var r,arr = [];
  while(arr.length<=qty) {    
    r = Math.floor(Math.random() * (to - from + 1) + from);
    if(!arr.indexOf(r)) arr.push(r);    
  }
  return arr;
 }

Open in new window

0
 

Author Comment

by:hibbsusan
ID: 37758374
this freezes the browser for me

http://jsfiddle.net/loren_hibbard/FkaSj/
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37758501
Can you change from mootools to plain js?
I cannot test jsfiddle on my iphone
0
 

Author Comment

by:hibbsusan
ID: 37758528
0
 

Author Comment

by:hibbsusan
ID: 37759077
for some reason, this works
if( (arr.indexOf(r)) === -1 ){ 
	
	arr.push(r); 
	
	}

Open in new window


dunno why it didn't before with just if ( !arr.indexOf(r))..
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37759436
Oops sorry. Yes -1 is the test
0
 

Author Closing Comment

by:hibbsusan
ID: 37768932
great. This was my final solution.

function randomFromInterval(from, to, qty, duplicates) {

    var r, arr = [];

    while (arr.length <= qty) {

        r = Math.floor(Math.random() * (to - from + 1) + from);

        if (!duplicates) {
           
            if ( (to-from) < qty ) {
               
                alert('not enough to choose from');
               
                return false;  
               
            }

            if ((arr.indexOf(r)) === -1) {

                arr.push(r);

            }

        } else {
           
            arr.push(r);
           
        }
    }

    return arr;

}
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 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

14 Experts available now in Live!

Get 1:1 Help Now