During the interview today, the interviewer came up with a question about the lottery system. Actually, I was a little confused and did not do well in the interview. After the interview, I wrote for more than an hour and solved some bugs. The overall feeling is: 1. Their code ability is really poor ah 2. A bug-free program is really difficult to write….
The rules are as follows: 1. Prizes are set for 1~10 prizes, one first prize, two second prize, three third prize…. 2. Each person can win the prize repeatedly, but the two adjacent prizes can not win the prize. For example, xiao Li can not win the second prize after winning the first prize, but can win the third prize. Xiao Hong who won the fourth prize can no longer get the fifth prize but can get the sixth prize.
function lotteryDraw(arr) { let result = {} let len = arr.length for (let i = 0; i < 10; I++) {let randomArr = [] let deepCopyArr = [...arr] index < len; index++) { let rIndex = Math.floor(Math.random() * deepCopyArr.length) randomArr[index] = deepCopyArr[rIndex] Deepcopyarr.splice (rIndex, 1)} let key = '${I + 1} let newArr = []; Let beforeArr = result ${I} [` award `] | | [] / / get the last of the winners for the let k = 0; k < i + 1; K++) {let flag = true // let rIdx while (flag) { RIdx = math.floor (math.random () * randomarr.length) beforeArr.foreach (ele => {if (randomArr[rIdx]] == ele) flag = true }); } newArr[k] = randomArr[rIdx] randomarr.splice (rIdx, 1) Result [key] = newArr} return result} //----- test ----- let arr5 = [] for (let I = 0; i < 50; i++) { arr5[i] = i.toString() } console.log(lotteryDraw(arr5));Copy the code
The following output is displayed: