Solved

javascript array without duplicates

Posted on 2012-03-22
17
333 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using VBScript. How to obtain the recomended paging file size? 8 52
Scroll 5 news at a time. 4 27
Jquery 2 12
QQ problem 22 23
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

777 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