-
-
Save codecademydev/f57d3c676295d8fff39e831e80353602 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Foursquare API Info | |
const clientId = 'PFUGRAI4R03K5FLNY2HXTV35I1HSQK14FTG4PWA22WQJMNCD'; | |
const clientSecret = 'XXZDA2PMIJI2BE41Z02A2ZDSL4XZHQE5TDF1OIZJ4BJZ0VZ2'; | |
const url = 'https://api.foursquare.com/v2/venues/explore?near='; | |
// APIXU Info | |
const apiKey = '621bcbc4f7124f81a0f110122191305'; | |
const forecastUrl = 'https://api.apixu.com/v1/forecast.json?key='; | |
// Page Elements | |
const $input = $('#city'); | |
const $submit = $('#button'); | |
const $destination = $('#destination'); | |
const $container = $('.container'); | |
const $venueDivs = [$("#venue1"), $("#venue2"), $("#venue3"), $("#venue4"), $("#venue5")]; | |
const $weatherDivs = [$("#weather1"), $("#weather2"), $("#weather3"), $("#weather4"), $("#weather5")]; | |
const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | |
// Add AJAX functions here: | |
const getVenues = async() => { | |
const city = $input.val(); | |
const urlToFetch = `${url}${city}&limit=10&client_id=${clientId}&client_secret=${clientSecret}&v=20190513`; | |
try { | |
const response = await fetch(urlToFetch); | |
if(response.ok) { | |
const jsonResponse = await response.json(); | |
console.log(jsonResponse); | |
const venues = jsonResponse.response.groups[0].items.map(item => item.venue); | |
console.log(venues); | |
return venues; | |
} else { | |
throw new Error('Request failed!'); | |
} | |
} catch(error) { | |
console.log(error.message); | |
} | |
} | |
const getForecast = async() => { | |
const urlToFetch = `${forecastUrl}${apiKey}&q=${$input.val()}&days=5&hour=11`; | |
try{ | |
const response = await fetch(urlToFetch); | |
if(response.ok) { | |
const jsonResponse = await response.json(); | |
console.log(jsonResponse); | |
const days = jsonResponse.forecast.forecastday; | |
return days; | |
} else {throw new Error('Request failed!');} | |
} catch(error) { | |
console.log(error.message); | |
} | |
} | |
// Render functions | |
const renderVenues = (venues) => { | |
$venueDivs.forEach(($venue, index) => { | |
// Add your code here: | |
const randomIndex = generateRandomResults(venues.length) | |
const venue = venues[randomIndex[index]]; | |
const venueIcon = venue.categories[0].icon; | |
const venueImgSrc = `"${venueIcon.prefix}bg_64${venueIcon.suffix}"`; | |
let venueContent = `<h2>${venue.name}</h2> | |
<img class="venueimage"src=${venueImgSrc}/> | |
<h3>Address:</h3> | |
<p>${venue.location.address}</p> | |
<p>${venue.location.city}</p> | |
<p>${venue.location.country}</p>`; | |
$venue.append(venueContent); | |
}); | |
$destination.append(`<h2>${venues[0].location.city}</h2>`); | |
} | |
const generateRandomResults = numOfResults => { | |
const randomIndexArray = []; | |
arrayLength = numOfResults < 4 ? numOfResults : 4; | |
while(randomIndexArray.length < arrayLength){ | |
const randomNumber = Math.floor(Math.random() * numOfResults); | |
if(!randomIndexArray.includes(randomNumber)){ | |
randomIndexArray.push(randomNumber); | |
} | |
} | |
return randomIndexArray; | |
} | |
const renderForecast = (days) => { | |
$weatherDivs.forEach(($day, index) => { | |
// Add your code here: | |
const currentDay = days[index]; | |
let weatherContent = createWeatherHTML(currentDay); | |
$day.append(weatherContent); | |
}); | |
} | |
const executeSearch = () => { | |
$venueDivs.forEach(venue => venue.empty()); | |
$weatherDivs.forEach(day => day.empty()); | |
$destination.empty(); | |
$container.css("visibility", "visible"); | |
getVenues().then(venues => renderVenues(venues)); | |
getForecast().then(forecast => renderForecast(forecast)); | |
return false; | |
} | |
$submit.click(executeSearch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment