数组除重:
新建一个空数组,利用中间状态,遇到相同数字时,其状态为1时跳出循环,新数组内没有则压入数组中。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html> <script> arr=[4,4,4,4,4,5,6,6,6,7,7,9,9,0,5,3,3,] for(i=0;i<arr.length;i++){ for(a=i+1;a<arr.length;a++){ if(arr[a]==arr[i]){ arr[i]="a" } } } arr1=[] for(b=0;b<arr.length;b++){ if(arr[b]!="a"){ arr1.push(arr[b]) } } for(var x in arr1){ document.write(arr1[x]+","); } </script>
随机点名:
利用Math.random()写一个x人为x*将随机数的范围限定为1-x之间,再用Math.floor()将所得的数进行取整,然后新建一个数组,将人名放入其中,用x*Math.random()求出随机人名
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html> <script> a=Math.random() b=10*a c=Math.floor(b) arr=["孙桂林","孙勃","孙振坤","刘裕泽","赵东","高鲁强","司书旭","周新","李晓彤","郭鹏"] var a=new Date() b=10*Math.random() alert(arr[c]) </script>