If we want to avoid seeing the background revealed by the rounded corners, we just need to also round the same corners of the <div> that contains the image (in this case, the <div class="category">s).
Review: Video Games API
async function fetchGames(searchTerm) {
// Disable button clicks while fetching from the API.
button.disabled = true;
button.textContent = 'Loading...';
let url = ``;
const allGames = [];
while (url != null) {
const response = await fetch(url);
const data = await response.json();
const someGames = data.results;
const gamesWithBgImages = someGames.filter(game => game.background_image != null);
allGames.push(...gamesWithBgImages);
url = data.next;
}
// Re-enable the button clicks after fetching.
button.disabled = false;
button.textContent = 'Submit';
return allGames;
}
If we want to only get the pages of the responses that have valid results:
let page = 1;
while (true) {
let url = `api.rawg.io/api/games?search=${searchTerms}&page=${page}`;
}
Our data comes back as an object, and within this object, we want the array stored at the `businesses` property.
So the array is essentially `data.businesses`.
Each element of this array is an object representing each business.
Let's use `.map` to create an array with objects only containing info that we want:
```js
async function getRestaurants(brand, location) {
// other code... //
const formattedBusinesses = data.businesses.map((business) => {
return {
id: business.id,
name: business.name,
rating: business.rating,
latitude: business.coordinates.latitude,
longitude: business.coordinates.longitude,
address: business.location.display_address.join(', '),
};
});
return formattedBusinesses;
}
Define another async function that finds Burger Kings in Miami:
What if the user types in "ebio34$T", "@#ERGIS", and "__*Y(sf889hf" in the input fields?
The API will not give an "OK" response.
So we can add a conditional check to only format incoming data that is valid.
if (response.ok) {
// Format the restaurant data
} else {
// Show a message on the web page to the user.
// Return out of the loop with whatever we have in the results array.
return allRestaurants;
}
.sort() method
This is a built-in JS function that sorts elements in an array.
function exportToCSV(restaurants) {
let csvContent = 'data:text/csv;charset=utf-8,';
// `\n` adds a new line
csvContent += `'id','name','address','rating'\n`;
restaurants.forEach(restaurant => {
// Note the comma separation and the new line.
csvContent += `${restaurant.id},${restaurant.name},${restaurant.address},${restaurant.rating}\n`;
});
const encodeUri = encodeURI(csvContent);
window.open(encodeUrl);
}
You'll be downloading a CSV file made out your array!
Morning Exercise (Popeyes)
(Similar to the Ninja Turtles exercise yesterday. Refer to yesterday's notes if necessary.)
await
s a fetch call to the API.async
function thatawait
s the invocation of the previous function.<div>
s.<div>
out of each item.<div>
s the class name "category".<div>
:<img>
withsrc
pointing to the image URL andalt
with a brief description of the image<h2>
whose text content is the item's name<div>s
into the "main" element.We made the
<div>
into fixed sizes, but the images are spilling out due to their various and big sizes.Let's make each image fill up only the width of each
<div>
that contains it:But the images have different heights, so they were either stretched or compressed vertically.
How can we avoid that?
To remove the underline of text with links:
Round the corners of boxes:
What if we just want the top two corners to be rounded?
If we want to avoid seeing the background revealed by the rounded corners, we just need to also round the same corners of the
<div>
that contains the image (in this case, the<div class="category">
s).Review: Video Games API
If we want to only get the pages of the responses that have valid results:
Spread Operator
It's like taking the contents of an array/object and shaking them out.
Yelp API Exercise Review
Remember to bring in
fetch
into Node on the top of the JS file:In order to let Yelp know that we're authorized to use this API:
Authorization
property withinheaders
async function getRestaurants(brand, location) { const url =
${baseUrl}${brand}&location=${location}
;const response = await fetch(url, { headers: { Authorization:
Bearer ${process.env.YELP_API_KEY}
, } });const data = await response.json(); }
Define another
async
function that finds Burger Kings in Miami:Important note! This API gives you more results than just Burger King, but we're only concerned with BKs.
So let's filter out the non-BK results:
Getting form values upon submit
Let's make a form:
It looks like this:
Use JS to target the fields and handle the submit event:
The helper function that renders
<tr>
s:Handling invalid inputs
What if the user types in "ebio34$T", "@#ERGIS", and "__*Y(sf889hf" in the input fields?
The API will not give an "OK" response.
So we can add a conditional check to only format incoming data that is valid.
.sort()
methodThis is a built-in JS function that sorts elements in an array.
To sort in ascending order:
To sort in descending order:
Exporting to CSV
The article Andy landed on from Google searching: https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side
Write a function that processes a JS array
You'll be downloading a CSV file made out your array!
encodeURI
function here