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
Solved

javascript array without duplicates

Posted on 2012-03-22
17
334 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 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