Solved

javascript array without duplicates

Posted on 2012-03-22
17
338 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
[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
  • 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:hibbsusan
ID: 37755085
leakim971,

That seems very simple and really great! Thanks!

How does prototype work?
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
 
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: 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

734 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