原始版:
1 <script type="text/javascript">
2 var srt='iphooonnne';
3 var newArr=[];
4 var i=0;
5 var j=0;
6
7 var strArr=srt.split('');
8
9 function toCompare(term,obj){
10 var fettle=true;
11
12 for(i=0; i<obj.length; i++){
13 if(obj[i]==term){
14 fettle=false;
15 }
16 }
17
18 if(fettle){
19 obj.push(term);
20 }
21 }
22
23 for(j=0; j<strArr.length; j++){
24
25 toCompare(strArr[j],newArr);
26 }
27
28 newArr=newArr.join('');
29
30 document.write(srt+'<br />');
31 document.write(strArr+'<br />');
32 document.write(newArr);
33 </script>
封装版:
1 <script type="text/javascript">
2 function toCompare(starStr){
3 var srt=starStr;
4 var newArr=[];
5 var i=0;
6 var j=0;
7
8 var strArr=srt.split('');
9
10 function toDispose(term,obj){
11 var fettle=true;
12
13 for(i=0; i<obj.length; i++){
14 if(obj[i]==term){
15 fettle=false;
16 }
17 }
18
19 if(fettle){
20 obj.push(term);
21 }
22 }
23
24 for(j=0; j<strArr.length; j++){
25
26 toDispose(strArr[j],newArr);
27 }
28
29 newArr=newArr.join('');
30
31 document.write(newArr);
32 }
33
34 toCompare('iphooonnne');
35 </script>