<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> var str = ‘‘; var add =[1,2,3,4,5,6,8,9]; var newArr = [1,2,3,4,5,6,7,8,9,-1,-2,-3,-4,-5,-6,-7,-8,-9]; function getN(arr){ return Math.floor(Math.random() * arr.length + 1)-1; } var resultArr = []; var log = ‘‘; function randNum(){ for(var i =0;i<80;i++){ var num = newArr[getN(newArr)];//随机取了一个数字 Math.abs(aaa); if(num < 0){//负数 if(inArray(Math.abs(num),resultArr) != -1 && (repeatNum(num,resultArr) != repeatNum(Math.abs(num),resultArr))){//正数存在,而且正负是不成对 resultArr.push(num); } else {//正数不存在 resultArr.push(-num); } } else {//正数 if((repeatNum(-num,resultArr) == repeatNum(Math.abs(num),resultArr))){//正数存在,而且正负成对 resultArr.push(num); } else {//正数不存在 resultArr.push(-num); } } } str += resultArr.join(‘,‘) + ‘|‘ + check(resultArr) + ‘<br>‘; } function getArr(n){ for(var i = 0; i<n; i++){ resultArr.length =0; randNum(); } } //查找索引 function inArray(t,arr){ if(arr.indexOf){ return arr.indexOf(t); } for(var i=arr.length;i--;){ if(arr[i]===t){ return i*1; } }; return -1; } //删除数组某一个 function deleInArray(n,arr){ if(n<0) return false; arr.splice(n,1); } //重复次数 function repeatNum(t,arr){ var times = 0; for(var i = 0;i<arr.length;i++){ if(t == arr[i]){ times ++; } } return times; } function addFunc(){ var newNum = newArr[getN(add)]; if(inArray(newNum,resultArr) == -1){//不存在 resultArr.push(newNum); } else { addFunc(); } } //检查数据 function check(arr){ var maxNum = 0; var str = ‘‘; for(var i = 0; i<arr.length; i++){ maxNum += arr[i]; if(maxNum >= 45){ str = ‘不可用‘ + ‘|‘ + ‘第‘ + i + ‘个数字‘ + ‘|‘ + ‘数字是:‘ + arr[i]; break; } else { str = ‘可用‘; } } return str; } getArr(100); document.write(str); </script> </body> </html>
目的是产生,数组长度是80的序列。正负相抵消,最后只剩下1到9就当作是中奖!注意要先产生正的才能出现负的!
时间: 2024-10-07 15:17:01