Skip to content

Instantly share code, notes, and snippets.

@evanrolfe
Created June 27, 2020 15:36
Show Gist options
  • Save evanrolfe/83a5c05bb4bc99df57650d28ac7ceaf3 to your computer and use it in GitHub Desktop.
Save evanrolfe/83a5c05bb4bc99df57650d28ac7ceaf3 to your computer and use it in GitHub Desktop.
// This function should retrieve the JSON from the `countryURL` and then call onCountryDataReceived() with the JSON
function getData(countryURL) {
fetch(countryURL)
.then((response) => {
return response.json();
})
.then((countries) => {
onCountryDataReceived(countries[0]);
})
}
function onCountryDataReceived(x) {
addCountryName(x);
addCountryCapital(x);
addNameInOtherLanguages(x);
}
// This function should take the JSON for the country and put a H1 tag on the screen containing its name
function addCountryName(countryData) {
const nameTag = document.createElement('h3');
nameTag.textContent = countryData.name;
document.getElementById('content').appendChild(nameTag);
}
// This function should take the JSON for the country and put a H2 tag on the screen containing its capital city
function addCountryCapital(countryData) {
const capitalTag = document.createElement('p');
capitalTag.textContent = countryData.capital;
document.getElementById('content').appendChild(capitalTag);
}
// This function should take the JSON for the country and put UL and LI tags on the screen with the countries name translated into other languages
function addNameInOtherLanguages(countryData) {}
function getContentDiv() {
return document.querySelector("#content");
}
function onLoad() {
getData(
"https://restcountries.eu/rest/v2/name/Great%20Britain?fullText=true"
);
/** Remove this line when you have completed the task
getData("https://restcountries.eu/rest/v2/name/France?fullText=true");
getData("https://restcountries.eu/rest/v2/name/Germany?fullText=true");
getData("https://restcountries.eu/rest/v2/name/Spain?fullText=true");
getData("https://restcountries.eu/rest/v2/name/Portugal?fullText=true");
getData("https://restcountries.eu/rest/v2/name/Hungary?fullText=true");
getData("https://restcountries.eu/rest/v2/name/Russia?fullText=true");
*/ // Remove this line when you have completed the task
}
window.onload = onLoad;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment