bibekyess commited on
Commit
c0d2247
·
1 Parent(s): a8c2362

Changes shuffling logic on seed

Browse files
Files changed (1) hide show
  1. script.js +27 -13
script.js CHANGED
@@ -1,3 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  async function fetchRestaurants() {
2
  const response = await fetch('restaurants.json');
3
  if (!response.ok) {
@@ -7,22 +31,11 @@ async function fetchRestaurants() {
7
  return data;
8
  }
9
 
10
- function seededRandom(seed) {
11
- var x = Math.sin(seed++) * 10000;
12
- return x - Math.floor(x);
13
- }
14
 
15
  function getRandomRestaurants(restaurants, seed) {
16
  // Generate three random indices based on the seed
17
- const indices = [];
18
- for (let i = 0; i < 3; i++) {
19
- const randomIndex = Math.floor(seededRandom(seed + i) * restaurants.length);
20
- indices.push(randomIndex);
21
- }
22
-
23
- // Get the corresponding restaurants
24
- const randomRestaurants = indices.map(index => restaurants[index]);
25
- return randomRestaurants;
26
  }
27
 
28
  async function displayRestaurants() {
@@ -59,6 +72,7 @@ async function displayRestaurants() {
59
  const restaurants = await fetchRestaurants();
60
  console.log('Fetched Restaurants:', restaurants); // Debugging: Print fetched restaurants
61
  const seed = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate(); // Create a seed from the date
 
62
  const randomRestaurants = getRandomRestaurants(restaurants, seed);
63
  console.log('Random Restaurants:', randomRestaurants); // Debugging: Print random restaurants
64
  document.getElementById('restaurants').innerHTML = randomRestaurants.map(restaurant =>
 
1
+ function shuffle(array, seed) { // <-- ADDED ARGUMENT
2
+ var m = array.length, t, i;
3
+
4
+ // While there remain elements to shuffle…
5
+ while (m) {
6
+
7
+ // Pick a remaining element…
8
+ i = Math.floor(random(seed) * m--); // <-- MODIFIED LINE
9
+
10
+ // And swap it with the current element.
11
+ t = array[m];
12
+ array[m] = array[i];
13
+ array[i] = t;
14
+ ++seed // <-- ADDED LINE
15
+ }
16
+
17
+ return array;
18
+ }
19
+
20
+ function random(seed) {
21
+ var x = Math.sin(seed++) * 10000;
22
+ return x - Math.floor(x);
23
+ }
24
+
25
  async function fetchRestaurants() {
26
  const response = await fetch('restaurants.json');
27
  if (!response.ok) {
 
31
  return data;
32
  }
33
 
 
 
 
 
34
 
35
  function getRandomRestaurants(restaurants, seed) {
36
  // Generate three random indices based on the seed
37
+ randomRestaurants = shuffle(restaurants, seed);
38
+ return randomRestaurants.slice(0,3);
 
 
 
 
 
 
 
39
  }
40
 
41
  async function displayRestaurants() {
 
72
  const restaurants = await fetchRestaurants();
73
  console.log('Fetched Restaurants:', restaurants); // Debugging: Print fetched restaurants
74
  const seed = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate(); // Create a seed from the date
75
+ console.log('Random seed per day: ', seed);
76
  const randomRestaurants = getRandomRestaurants(restaurants, seed);
77
  console.log('Random Restaurants:', randomRestaurants); // Debugging: Print random restaurants
78
  document.getElementById('restaurants').innerHTML = randomRestaurants.map(restaurant =>