This repository was archived by the owner on Feb 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (70 loc) · 2.09 KB
/
Copy pathscript.js
File metadata and controls
90 lines (70 loc) · 2.09 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Inital items
const items = [
{name: 'Bike', selected: false},
{name: 'Car', selected: false},
{name: 'Plane', selected: false},
{name: 'Boat', selected: false},
{name: 'Train', selected: false},
{name: 'Bus', selected: false},
{name: 'Subway', selected: false},
{name: 'Motorcycle', selected: false},
]
function shuffle(array) {
// Fisher-Yates (Knuth) Shuffle algorithm
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
return array;
}
// Separate items in groups of 2
const separateItems = (items) => {
const groups = []
for (let i of items) {
let index = items.indexOf(i)
if (index % 2 !== 0 && (items.length % 2 === 0)) continue
groups.push([items[index], items[items.indexOf(i) + 1]])
if (items.indexOf(i) === (items / 2) - 1) {
break
}
}
return groups
}
const separateMoreGroups = (groups) => {
// Split groups
let array = []
groups.forEach(group => group.forEach(item => array.push(item)))
// Remove no selected items
array = array.filter(group => group.selected)
// Define selected false for all items
array = array.map(group => {
return {...group, selected: false}
})
if (groups.length === 1) {
return []
}
return separateItems(array)
}
const suffleItems = shuffle(items)
const groups = separateItems(suffleItems)
let groupsShow = groups
let selected = []
while(groupsShow.length > 0) {
groupsShow[0][0].selected = true
selected.push(groupsShow[0][0])
// Remove the first group
groupsShow.shift()
// Separate more groups
if (groupsShow.length === 0 && selected.length >= 2) {
groupsShow = separateItems(selected)
console.log('NOVA RODADA')
selected = []
}
}
console.log(`O selecionado foi ${selected.map(item => item.name)}`)
console.log(separateMoreGroups(groupsShow))