Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 5 of 5
Thread: Why is this function crashing???

06182012, 12:56 PM #1
 Join Date
 Jun 2012
 Posts
 2
 Thanks
 0
 Thanked 0 Times in 0 Posts
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 11, 22, 33 and 44, and you want to swap one element of each pair and give it to another
example
14, 23, 31, 4,2
the only thing I want to make sure is:
1. No element can be paired with 'himself' so no '11 or 22' after the function.
and 2. No repeating assigned elements, so no '13, 23' 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=b1){ if (x[a] == x[b]  x[a]==a){ do { x[a]= Math.floor(Math.random()*value); } while(x[a] == a); b=a; } else { } }
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; 06182012 at 03:05 PM.

06182012, 04:21 PM #2
 Join Date
 Jan 2010
 Location
 Behind the Wall
 Posts
 4,237
 Thanks
 14
 Thanked 441 Times in 437 Posts
if it crashes, it should leave a message in the Error Console
The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.André Behrens, NY Times Software Developer

06182012, 09:48 PM #3
 Join Date
 Jan 2011
 Location
 Washington
 Posts
 5,799
 Thanks
 27
 Thanked 794 Times in 792 Posts
Maybe because it's missing two curly brackets at the end
}}
If not fixed publish the rest of the code

06182012, 10:55 PM #4
 Join Date
 Jun 2012
 Posts
 2
 Thanks
 0
 Thanked 0 Times in 0 Posts
thanks,
seems I had some bugs in another part of the code.

06192012, 01:23 AM #5
Your script craches when the last pair cannot be made.
e.g. if the first 3 pairs becomes 12 , 24 , 41.
then the only possible pair left is 33.
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 secondvalue and the remaining secondvalue 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.length1;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.length2)); if (Pairs[Pairs.length1] == SecondValues[0]){ Pairs[Pairs.length1]=[Pairs[Pairs.length1],Pairs[S][1]]; Pairs[S][1]=SecondValues[0]; }else{ Pairs[Pairs.length1]=[Pairs[Pairs.length1],SecondValues[0]] } </script>
Code:Pairs = [[3, 1], [2, 4], [4, 3], [1, 2]]