Solved

javascript array without duplicates

Posted on 2012-03-22
17
328 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 …
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

13 Experts available now in Live!

Get 1:1 Help Now