Spaces:
Running
Running
const restaurant_no = 5; | |
function shuffle(array, seed) { | |
var m = array.length, t, i; | |
while (m) { | |
i = Math.floor(random(seed) * m--); | |
t = array[m]; | |
array[m] = array[i]; | |
array[i] = t; | |
++seed | |
} | |
return array; | |
} | |
function random(seed) { | |
var x = Math.sin(seed++) * 10000; | |
return x - Math.floor(x); | |
} | |
async function fetchRestaurants() { | |
const response = await fetch('restaurants.json'); | |
if (!response.ok) { | |
throw new Error('Network response was not ok ' + response.statusText); | |
} | |
const data = await response.json(); | |
return data; | |
} | |
function getRandomRestaurants(restaurants, seed) { | |
randomRestaurants = shuffle(restaurants, seed); | |
return randomRestaurants.slice(0,restaurant_no); | |
} | |
function getDailySeed() { | |
const today = new Date(); | |
return today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate(); | |
} | |
async function displayRestaurants(useRandomSeed = false) { | |
const today = new Date(); | |
const day = today.getDay(); | |
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; | |
const formattedDate = today.toLocaleDateString(undefined, options); | |
document.getElementById('currentDate').innerText = `Today is ${formattedDate}`; | |
if (day === 0 || day === 6) { | |
document.getElementById('restaurants').innerHTML = "No suggestions available today!"; | |
return; | |
} | |
try { | |
const restaurants = await fetchRestaurants(); | |
const seed = useRandomSeed ? Math.floor(Math.random() * 1000000) : getDailySeed(); | |
const randomRestaurants = getRandomRestaurants(restaurants, seed); | |
const firstThreeRestaurants = randomRestaurants.slice(0, 3); | |
const bonusRestaurants = randomRestaurants.slice(3, 5); | |
document.getElementById('restaurants').innerHTML = firstThreeRestaurants.map(restaurant => | |
`<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>` | |
).join('<br>'); | |
document.getElementById('bonusRestaurants').innerHTML = bonusRestaurants.map(restaurant => | |
`<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>` | |
).join('<br>'); | |
// Remove previous suggest again button | |
const existingButton = document.querySelector('.suggest-again-button'); | |
if (existingButton) { | |
existingButton.remove(); | |
} | |
// Add new suggest again button | |
const suggestButton = document.createElement('div'); | |
suggestButton.classList.add('suggest-again-button'); | |
suggestButton.style.cssText = 'text-align: center; margin-top: 20px;'; | |
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>'; | |
document.querySelector('.bonus-container').after(suggestButton); | |
} catch (error) { | |
const bestRestaurants = [ | |
["๋๋น๊ณ ๊ณ (unlimited ๋๊น์ค + ์ ์ก (Pork) for 8.8K)", "https://naver.me/GGUfwvl9"], | |
["Taksim Kebab (Turkish, small place)", "https://maps.app.goo.gl/6rwGVo5qbT9xKZAMA"], | |
["๊ฐ๋จ์ญ ํ์คํ (Pasta, large space)", "https://naver.me/xL1EJLfC"], | |
] | |
document.getElementById('restaurants').innerHTML = "Failed to load restaurant suggestions. <br>Below are my personal suggestions based on your review!!"; | |
document.getElementById('bonusHeader').innerHTML = ""; | |
document.getElementById('bonusRestaurants').innerHTML = bestRestaurants.map(restaurant => | |
`<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>` | |
).join('<br>'); | |
// Remove previous suggest again button | |
const existingButton = document.querySelector('.suggest-again-button'); | |
if (existingButton) { | |
existingButton.remove(); | |
} | |
// Add new suggest again button | |
const suggestButton = document.createElement('div'); | |
suggestButton.classList.add('suggest-again-button'); | |
suggestButton.style.cssText = 'text-align: center; margin-top: 20px;'; | |
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>'; | |
document.querySelector('.bonus-container').after(suggestButton); | |
} | |
} | |
window.onload = () => displayRestaurants(false); |