bibekyess commited on
Commit
8626d26
·
1 Parent(s): a8504b7

Bug fix for getRandomRestaurants

Browse files
Files changed (1) hide show
  1. script.js +7 -7
script.js CHANGED
@@ -7,12 +7,10 @@ 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
  // Shuffle using seeded random
17
  for (let i = restaurants.length - 1; i > 0; i--) {
18
  const j = Math.floor(seededRandom(seed + i) * (i + 1));
@@ -24,6 +22,9 @@ function getRandomRestaurants(restaurants, seed) {
24
  async function displayRestaurants() {
25
  const today = new Date();
26
  const day = today.getDay();
 
 
 
27
 
28
  // Format the date
29
  const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
@@ -54,8 +55,7 @@ async function displayRestaurants() {
54
  try {
55
  const restaurants = await fetchRestaurants();
56
  console.log('Fetched Restaurants:', restaurants); // Debugging: Print fetched restaurants
57
- const seed = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate(); // Create a seed from the date
58
- const randomRestaurants = getRandomRestaurants(restaurants, seed);
59
  console.log('Random Restaurants:', randomRestaurants); // Debugging: Print random restaurants
60
  document.getElementById('restaurants').innerHTML = randomRestaurants.map(restaurant =>
61
  `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
 
7
  return data;
8
  }
9
 
10
+ function getRandomRestaurants(restaurants, year, month, day) {
11
+ // Generate a seed based on the current date and time
12
+ const seed = year * 10000 + month * 100 + day;
 
13
 
 
14
  // Shuffle using seeded random
15
  for (let i = restaurants.length - 1; i > 0; i--) {
16
  const j = Math.floor(seededRandom(seed + i) * (i + 1));
 
22
  async function displayRestaurants() {
23
  const today = new Date();
24
  const day = today.getDay();
25
+ const year = today.getFullYear();
26
+ const month = today.getMonth() + 1; // Months are zero-based
27
+ const date = today.getDate();
28
 
29
  // Format the date
30
  const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
 
55
  try {
56
  const restaurants = await fetchRestaurants();
57
  console.log('Fetched Restaurants:', restaurants); // Debugging: Print fetched restaurants
58
+ const randomRestaurants = getRandomRestaurants(restaurants, year, month, date);
 
59
  console.log('Random Restaurants:', randomRestaurants); // Debugging: Print random restaurants
60
  document.getElementById('restaurants').innerHTML = randomRestaurants.map(restaurant =>
61
  `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`