I have prepared a password generation tool consisting of 0-9 and a-k English letters. The numbers are in disorder. Replace a random number with a letter to generate a password of 10.
- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
- Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
- <Html xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> </title>
- </Head>
- <Body>
- <Div>
- <Input id = "strat" type = "button" value = "start"/>
- <Textarea rows = "10" cols = "25" id = "password"> </textarea>
- </Div>
- <Script type = "text/javascript">
- Void function (window, doc, undefined ){
- Var array = [], text = 'abcdefghjk ', key, password = '', btn = doc. getElementById ('strat'), textarea = doc. getElementById ('Password ');
- Function unorderedArray (array ){
- If ({}). toString. call (array). indexOf ('array') ===-1) return;
- Var arr = [], value = array. toString ();
- Arr = array. sort (function () {return Math. random ()> 0.5? -1:1 ;});
- (Arr. toString () === value) & arguments. callee (array );
- Return arr;
- }
- Btn. onclick = function (){
- Key = Math. random (). toFixed (1) * 10-1;
- Array = [,];
- Password = unorderedArray (array );
- Password [key] = text [key];
- Textarea. innerHTML = password. join ('');
- }
- } (Window, document );
- // Console. log (unorderedArray ());
- </Script>
- </Body>
- </Html>
How to sort arrays in disorder is a poor math. After searching, input such a function to the array sort.
Sort is really powerful.
- function unorderedArray(array){
- if(({}).toString.call(array).indexOf('Array')===-1) return;
- var arr=[],value=array.toString();
- arr=array.sort(function(){return Math.random()>0.5?-1:1;});
- (arr.toString()===value) && arguments.callee(array);
- return arr;
- }
When using this method, we should avoid the generation of arrays, which is the same as the input array. This is a waste of effort, because the array is of a complex type and all modifications will change. Therefore, a variable is used to cache the value of the input array = array. toString (), and then the value of the array I generated is compared if it is relatively recursive. Ensure that the generated array is not sequential.
Use the following code to generate a random number 0-9.
- Math.random().toFixed(1) * 10-1;
Use this serial number to get a letter from a-k, and use this letter to replace the numbers at the corresponding position of the serial number in the array.
Concatenate the obtained Array Using the join method of the array
- password.join('');
Obtain a key.
Original article: http://www.cnblogs.com/enix/archive/2011/12/22/2297913.html