ghost613 commited on
Commit
c35e5b5
·
verified ·
1 Parent(s): ae75435

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +29 -39
script.js CHANGED
@@ -2,19 +2,26 @@ const restaurant_no = 5;
2
 
3
  function shuffle(array, seed) {
4
  var m = array.length, t, i;
 
 
5
  while (m) {
 
 
6
  i = Math.floor(random(seed) * m--);
 
 
7
  t = array[m];
8
  array[m] = array[i];
9
  array[i] = t;
10
  ++seed
11
  }
 
12
  return array;
13
- }
14
-
15
  function random(seed) {
16
- var x = Math.sin(seed++) * 10000;
17
- return x - Math.floor(x);
18
  }
19
 
20
  async function fetchRestaurants() {
@@ -26,32 +33,40 @@ async function fetchRestaurants() {
26
  return data;
27
  }
28
 
 
29
  function getRandomRestaurants(restaurants, seed) {
 
30
  randomRestaurants = shuffle(restaurants, seed);
31
  return randomRestaurants.slice(0,restaurant_no);
32
  }
33
 
34
- function getDailySeed() {
35
- const today = new Date();
36
- return today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate();
37
- }
38
-
39
- async function displayRestaurants(useRandomSeed = false) {
40
  const today = new Date();
41
  const day = today.getDay();
 
 
42
  const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
43
  const formattedDate = today.toLocaleDateString(undefined, options);
 
 
44
  document.getElementById('currentDate').innerText = `Today is ${formattedDate}`;
45
 
 
46
  if (day === 0 || day === 6) {
47
  document.getElementById('restaurants').innerHTML = "No suggestions available today!";
48
  return;
49
  }
50
 
 
 
51
  try {
52
  const restaurants = await fetchRestaurants();
53
- const seed = useRandomSeed ? Math.floor(Math.random() * 1000000) : getDailySeed();
 
 
54
  const randomRestaurants = getRandomRestaurants(restaurants, seed);
 
 
55
  const firstThreeRestaurants = randomRestaurants.slice(0, 3);
56
  const bonusRestaurants = randomRestaurants.slice(3, 5);
57
 
@@ -62,20 +77,8 @@ async function displayRestaurants(useRandomSeed = false) {
62
  `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
63
  ).join('<br>');
64
 
65
- // Remove previous suggest again button
66
- const existingButton = document.querySelector('.suggest-again-button');
67
- if (existingButton) {
68
- existingButton.remove();
69
- }
70
-
71
- // Add new suggest again button
72
- const suggestButton = document.createElement('div');
73
- suggestButton.classList.add('suggest-again-button');
74
- suggestButton.style.cssText = 'text-align: center; margin-top: 20px;';
75
- suggestButton.innerHTML = '<button onclick="displayRestaurants(true)" style="padding:8px 16px; background-color:#4CAF50; color:white; border:none; border-radius:4px; cursor:pointer;">Suggest Again</button>';
76
- document.querySelector('.bonus-container').after(suggestButton);
77
-
78
  } catch (error) {
 
79
  const bestRestaurants = [
80
  ["돈비고고 (unlimited 돈까스 + 제육 (Pork) for 8.8K)", "https://naver.me/GGUfwvl9"],
81
  ["Taksim Kebab (Turkish, small place)", "https://maps.app.goo.gl/6rwGVo5qbT9xKZAMA"],
@@ -85,21 +88,8 @@ async function displayRestaurants(useRandomSeed = false) {
85
  document.getElementById('bonusHeader').innerHTML = "";
86
  document.getElementById('bonusRestaurants').innerHTML = bestRestaurants.map(restaurant =>
87
  `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
88
- ).join('<br>');
89
-
90
- // Remove previous suggest again button
91
- const existingButton = document.querySelector('.suggest-again-button');
92
- if (existingButton) {
93
- existingButton.remove();
94
- }
95
-
96
- // Add new suggest again button
97
- const suggestButton = document.createElement('div');
98
- suggestButton.classList.add('suggest-again-button');
99
- suggestButton.style.cssText = 'text-align: center; margin-top: 20px;';
100
- suggestButton.innerHTML = '<button onclick="displayRestaurants(true)" style="padding:8px 16px; background-color:#4CAF50; color:white; border:none; border-radius:4px; cursor:pointer;">Suggest Again</button>';
101
- document.querySelector('.bonus-container').after(suggestButton);
102
  }
103
  }
104
 
105
- window.onload = () => displayRestaurants(false);
 
2
 
3
  function shuffle(array, seed) {
4
  var m = array.length, t, i;
5
+
6
+ // While there remain elements to shuffle…
7
  while (m) {
8
+
9
+ // Pick a remaining element…
10
  i = Math.floor(random(seed) * m--);
11
+
12
+ // And swap it with the current element.
13
  t = array[m];
14
  array[m] = array[i];
15
  array[i] = t;
16
  ++seed
17
  }
18
+
19
  return array;
20
+ }
21
+
22
  function random(seed) {
23
+ var x = Math.sin(seed++) * 10000;
24
+ return x - Math.floor(x);
25
  }
26
 
27
  async function fetchRestaurants() {
 
33
  return data;
34
  }
35
 
36
+
37
  function getRandomRestaurants(restaurants, seed) {
38
+ // Generate three random indices based on the seed
39
  randomRestaurants = shuffle(restaurants, seed);
40
  return randomRestaurants.slice(0,restaurant_no);
41
  }
42
 
43
+ async function displayRestaurants() {
 
 
 
 
 
44
  const today = new Date();
45
  const day = today.getDay();
46
+
47
+ // Format the date
48
  const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
49
  const formattedDate = today.toLocaleDateString(undefined, options);
50
+
51
+ // Display the date
52
  document.getElementById('currentDate').innerText = `Today is ${formattedDate}`;
53
 
54
+ // Only display suggestions on weekdays
55
  if (day === 0 || day === 6) {
56
  document.getElementById('restaurants').innerHTML = "No suggestions available today!";
57
  return;
58
  }
59
 
60
+ const dateKey = today.toISOString().split('T')[0];
61
+
62
  try {
63
  const restaurants = await fetchRestaurants();
64
+ console.log('Fetched Restaurants:', restaurants); // Debugging: Print fetched restaurants
65
+ const seed = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate(); // Create a seed from the date
66
+ console.log('Random seed per day: ', seed);
67
  const randomRestaurants = getRandomRestaurants(restaurants, seed);
68
+ console.log('Random Restaurants:', randomRestaurants); // Debugging: Print random restaurants
69
+
70
  const firstThreeRestaurants = randomRestaurants.slice(0, 3);
71
  const bonusRestaurants = randomRestaurants.slice(3, 5);
72
 
 
77
  `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
78
  ).join('<br>');
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  } catch (error) {
81
+ console.error('Error fetching restaurants:', error);
82
  const bestRestaurants = [
83
  ["돈비고고 (unlimited 돈까스 + 제육 (Pork) for 8.8K)", "https://naver.me/GGUfwvl9"],
84
  ["Taksim Kebab (Turkish, small place)", "https://maps.app.goo.gl/6rwGVo5qbT9xKZAMA"],
 
88
  document.getElementById('bonusHeader').innerHTML = "";
89
  document.getElementById('bonusRestaurants').innerHTML = bestRestaurants.map(restaurant =>
90
  `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
91
+ ).join('<br>') + "<br><br>";
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  }
93
  }
94
 
95
+ window.onload = displayRestaurants;