-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomfunction.js
More file actions
64 lines (55 loc) · 2.02 KB
/
Copy pathrandomfunction.js
File metadata and controls
64 lines (55 loc) · 2.02 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
function getRandomEntries(radioValue, maxValue, minValue) {
/* console.log("radioValuefunc:", radioValue);
console.log("minValuefunc:", minValue);
console.log("maxValuefunc:", maxValue);
*/
if (!radioValue || !minValue || !maxValue) {
throw new Error("Missing required parameters");
}
//Get the JSON data from the JSON file
fetch("./shop.json") // Replace with the correct path to your JSON file
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok " + response.statusText);
}
return response.json(); // Parse the response as JSON
})
.then((data) => {
// Save the JSON data into the variable
jsonData = data;
//console.log(jsonData); // Log the JSON data to the console or use it as needed
})
.catch((error) => {
console.error("Error fetching the JSON file:", error);
});
const randomCount = Math.floor(Math.random() * (25 - 5 + 1)) + 5;
const filteredData = jsonData.filter((entry) => {
if (radioValue === "ALL") {
return entry.VALORE >= minValue && entry.VALORE <= maxValue;
} else {
return (
entry.SHOP === radioValue &&
entry.VALORE >= minValue &&
entry.VALORE <= maxValue
);
}
});
//randomize the data
const randomizedData = filteredData.sort(() => Math.random() - 0.5);
//select the first randomCount elements
const selectedEntries = randomizedData.slice(0, randomCount);
//sort the selected entries by VALORE
const sortedData = selectedEntries.slice(); // create a copy of selectedEntries
for (let i = 0; i < sortedData.length - 1; i++) {
for (let j = 0; j < sortedData.length - i - 1; j++) {
if (sortedData[j].VALORE > sortedData[j + 1].VALORE) {
// swap elements
const temp = sortedData[j];
sortedData[j] = sortedData[j + 1];
sortedData[j + 1] = temp;
}
}
}
//console.log("selectedEntries:", sortedData);
return sortedData;
}