Asynchronous Javascript Practice questions
Q 1. You have been hired by a travel agency to create a small web app that displays travel
destinations. The data for these destinations is available from an external API in JSON
format. Your task is to fetch the data, process it, and display the destinations dynamically on
the web page. Fetch the travel destinations data from the following API endpoint:
[Link]
JSON Format:
[
{
"id": 1,
"name": "Paris",
"country": "France",
"description": "The city of light with iconic landmarks like the Eiffel Tower.",
"image": "[Link]
},
{
"id": 2,
"name": "Tokyo",
"country": "Japan",
"description": "A bustling city known for its modern architecture and cherry blossoms.",
"image": "[Link]
}
]
Create a <div> element with an ID of destinations to serve as a container for the
fetched data.
For each destination, dynamically create a card-like structure using JavaScript and
append it to the container. The card should include:
o The destination name and country (e.g., "Paris, France") as a heading.
o A description of the destination.
o An image representing the destination.
Style the cards minimally using CSS for better visual clarity (optional).
Handle potential errors:
If the API call fails, display an error message on the page.
If the fetched data is empty, display a message saying "No destinations available."
Q 2. You are working on a weather dashboard application. The
weather data for different cities is available through an external
API. Your task is to fetch the data and process it to display specific
information in the console. Fetch the weather data from the
following API endpoint:
[Link]
JSON Format:
[
{
"city": "New York",
"temperature": 25,
"condition": "Sunny",
"humidity": 60
},
{
"city": "London",
"temperature": 18,
"condition": "Cloudy",
"humidity": 80
},
{
"city": "Tokyo",
"temperature": 22,
"condition": "Rainy",
"humidity": 90
}
]
Using JavaScript, fetch the JSON data from the API. Parse the data to extract the following
details for each city:
o City name
o Temperature
o Weather condition
Log the details to the console in the following format:
City: New York, Temperature: 25°C, Condition: Sunny
City: London, Temperature: 18°C, Condition: Cloudy
City: Tokyo, Temperature: 22°C, Condition: Rainy