Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-18-2012, 11:56 AM   PM User | #1
andricom
New to the CF scene

 
Join Date: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
andricom is an unknown quantity at this point
Question Why is this function crashing???

Hi,

I'll try to explain what I was trying to do with this functions.

say you have four pairs of things, pair 1-1, 2-2, 3-3 and 4-4, and you want to swap one element of each pair and give it to another

example

1-4, 2-3, 3-1, 4,2

the only thing I want to make sure is:

1. No element can be paired with 'himself' so no '1-1 or 2-2' after the function.

and 2. No repeating assigned elements, so no '1-3, 2-3' after the function.

So this would be a way of permute numbers.

an this is how I tryed it:

Code:
value = document.myform.three.value;

var x = new Array(value);
  
  for(a=0;a<value;a++){
    
    x[a]= 0;
    }


function itera(){
  
value = document.myform.three.value;

  for(var a=0; a<value; a++){ 


    x[a] = Math.floor(Math.random()*value);
          

      for (var b=a; b>=0; b=b-1){
        
        
          if (x[a] == x[b] || x[a]==a){
          
            do {
            x[a]= Math.floor(Math.random()*value);
            }
            
            while(x[a] == a);
         
                    
          b=a;
        
                 
          }
      
          else {
      
        
          }
      
      }
being 'Value' the number of pairs to permute, which is an external input.

This function works fine some times but sometimes it crashes and I can't figure out why

I'd apreciate some help please. thanks.


PD: I don't care whether there is a specific function to permute numbers, I'd simply like to understand whats wrong with this one.

Last edited by andricom; 06-18-2012 at 02:05 PM..
andricom is offline   Reply With Quote
Old 06-18-2012, 03:21 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,865
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
if it crashes, it should leave a message in the Error Console
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 06-18-2012, 08:48 PM   PM User | #3
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Maybe because it's missing two curly brackets at the end
}}

If not fixed publish the rest of the code
sunfighter is offline   Reply With Quote
Old 06-18-2012, 09:55 PM   PM User | #4
andricom
New to the CF scene

 
Join Date: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
andricom is an unknown quantity at this point
thanks,

seems I had some bugs in another part of the code.
andricom is offline   Reply With Quote
Old 06-19-2012, 12:23 AM   PM User | #5
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
Your script craches when the last pair cannot be made.

e.g. if the first 3 pairs becomes 1-2 , 2-4 , 4-1.
then the only possible pair left is 3-3.
as it fails to validate, the script will try over and over again, but will never succeed.

In this case a swap between a random, already set second-value and the remaining second-value is needed

Code:
<script>
value = document.myform.three.value;

Pairs=[];
for(var a=0; a<value; a++){ 
 Pairs[a]=a+1;
}
SecondValues=Pairs.slice(0);
// --- Create all pairs but one --- //
for (x=0;x<Pairs.length-1;x++){
  do {
    C=Math.floor(Math.random()*SecondValues.length);
  } while(Pairs[x]==SecondValues[C] );

    Pairs[x]=[Pairs[x],SecondValues.splice(C,1)[0]];
}
// --- Test, and swap values if last pairs can't be valid. --- //
   S=Math.floor(Math.random()*(Pairs.length-2));
if (Pairs[Pairs.length-1] == SecondValues[0]){
   Pairs[Pairs.length-1]=[Pairs[Pairs.length-1],Pairs[S][1]];
   Pairs[S][1]=SecondValues[0];
}else{
Pairs[Pairs.length-1]=[Pairs[Pairs.length-1],SecondValues[0]]
}
</script>
this will create:
Code:
Pairs = [[3, 1], [2, 4], [4, 3], [1, 2]]
or alike;
Lerura is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:56 AM.


Advertisement
Log in to turn off these ads.