DrDaMour
04-18-2003, 01:27 PM
is there a next permutation functoin for a javascript array?
or does someone have a small script that will jumble up an array randomly?
or does someone have a small script that will jumble up an array randomly?
|
Click to See Complete Forum and Search --> : Array Next Permutation DrDaMour 04-18-2003, 01:27 PM is there a next permutation functoin for a javascript array? or does someone have a small script that will jumble up an array randomly? SniperX 04-29-2003, 06:27 AM Odds are this wont make sense to u, but u could use a for loop for the number of elements in the array, and use Math.random() * number of elements - and set that as the number as the new index for the array. Then run a check on the elements and if there are duplicate numbers just generate a new one. i.e. function randomize() { var num; for (var i=0; i<array.length(); i++) { num = Math.random() * array.length(); if (array[num] != null) { while(array[num] != null) { num Math.random() * array.length(); } } } } p.s. Im actually a c programmer so i dont know if that will work. Regards MW klepto 03-24-2004, 03:18 PM ok, i'm new at this. What i'm trying to do is input a 5 character string and output every single possible 3 character combination. For example, from the string "water", you could form wat, wae, war, wta, wte, wtr, wea, wet, wer, wra, wrt, wre, awt, awe, awr, and so on. I'm probidly missing something very basic. Here's what i got so far: var myarray = new Array(5); var word; function strarray() { myarray = myform.char.value; //validate string is 5 characters long if (myarray.length != 5) window.alert("You must enter only a 5 digit string!"); //nested loops to find all possible 3 character strings for (var x=0; x<5; ++x) for (var y=0; y<5; ++y) for (var z=0; z<5; ++z) //check for doubles if (x != y && y != z && z != x) word = myarray[x] + myarray[y] + myarray[z]; myform.output.value = word; } can any knowledgable soul out there offer any assistance? webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |