-
Notifications
You must be signed in to change notification settings - Fork 0
/
random-to-radom without loop.html
49 lines (45 loc) · 1.25 KB
/
random-to-radom without loop.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<script>
let winnerArray = [];
let itemsLeft = 10;
let runAmt = itemsLeft;
let items = {};
const addToArray = (random) => {
let randomResult = random % itemsLeft;
if (randomResult === 0) {
randomResult = 1;
}
if (itemsLeft === randomResult) {
if (items[randomResult] !== undefined) {
winnerArray.push(items[randomResult]);
console.log(1,randomResult,items,winnerArray);
} else {
winnerArray.push(itemsLeft);
console.log(2,randomResult,items,winnerArray);
}
} else {
if (items[randomResult] === undefined) {
let winner = randomResult;
winnerArray.push(winner);
console.log(3,randomResult,items,winnerArray);
if (items[itemsLeft] !== undefined) {
items[winner] = items[itemsLeft];
} else {
items[winner] = itemsLeft;
}
} else {
winnerArray.push(items[randomResult]);
console.log(4,randomResult,items,winnerArray);
if (items[itemsLeft] !== undefined) {
let itemLeftVal = items[itemsLeft];
items[randomResult] = itemLeftVal;
} else {
items[randomResult] = itemsLeft;
}
}
}
itemsLeft--;
};
for (let i = 0; i < runAmt; i++) {
addToArray(Math.floor(Math.random() * 10000000000000000000000) + 1);
}
</script>