You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.9 KiB
2.9 KiB
Day 18 - Promise
Exercise:Solutions
Home | << Day 17 | Day 19 >>
Exercise Solutions
Exercises: Level 1
- Read the countries API using fetch and print the name of country, capital, languages, population and area.
// app.js
const countriesAPI = 'https://restcountries.com/v2/all'
fetch('https://restcountries.com/v2/all')
.then(response => response.json())
.then(data => {
data.forEach(country => {
const name = country.name;
const capital = country.capital;
const languages = country.languages.map(language => language.name);
const population = country.population;
const region = country.region;
console.log(`${name}, başkenti ${capital}'dir. Nüfusu ${population} ve bölgesi ${region}. Konuşulan diller: ${languages.join(', ')}`);
});
});
Exercises: Level 2
- Print out all the cat names in to catNames variable.
// app.js
fetch('https://api.thecatapi.com/v1/breeds')
.then(response => response.json())
.then(data => {
const catNames = data.map(cat => cat.name);
console.log(catNames);
});
Exercises: Level 3
- Read the cats api and find the average weight of cat in metric unit.
// app.js
const catsAPI = 'https://api.thecatapi.com/v1/breeds'
fetch('https://api.thecatapi.com/v1/breeds')
.then(response => response.json())
.then(data => {
const weights = data.map(cat => cat.weight.metric);
console.log(weights);
});
- Read the countries api and find out the 10 largest countries
// app.js
fetch('https://restcountries.com/v2/all')
.then(response => response.json())
.then(data => {
data.sort((a, b) => b.area - a.area);
const top10 = data.slice(0, 10);
top10.forEach(country => {
const name = country.name;
const capital = country.capital;
const languages = country.languages.map(language => language.name);
const population = country.population;
const region = country.region;
console.log(`${name}, başkenti ${capital}'dir. Nüfusu ${population} ve bölgesi ${region}. Konuşulan diller: ${languages.join(', ')}`);
});
});
- Read the countries api and count total number of languages in the world used as officials.
// app.js
fetch('https://restcountries.com/v2/all')
.then(response => response.json())
.then(data => {
const languages = new Set();
data.forEach(country => {
country.languages.forEach(language => {
languages.add(language.name);
});
});
console.log(`Dünya genelinde resmi olarak kullanılan dillerin toplam sayısı: ${languages.size}`);
});