Project: Weather app
Fetch a city, format the data, and handle the city that does not exist.
Project 22 of 23 in JavaScript30 min2 exercises
The weather app is the classic first fetch project. The interesting part is not the happy path — it is what happens when the city is missing or the network fails.
// GET https://api.example.com/weather?city=London
{
"city": "London",
"tempC": 12,
"condition": "Rain"
}- 01Format — turn the raw object into a line of text, and decide what counts as freezing.
- 02Load — fetch, check
response.ok, and return a friendly message instead of throwing. - 03In the full app: an input, a submit handler, a loading state, and
catchfor network errors.
Exercises
Checked in your browser · hints and solutions included
01
Exercise
Checkpoint 1 — format the reading
Write two functions:
- formatWeather(data) returns
"<rounded temp>°C · <condition>", rounding withMath.round. Example:"12°C · Rain". - isFreezing(tempC) returns
truewhen the temperature is 0 or below.
Use the · character (middle dot) exactly as shown.
exercise.js
console · run your code or check the answer
02
Exercise
Checkpoint 2 — load a city
Write an async function loadCity(city) that fetches https://api.example.com/weather?city=<city> and:
- returns the formatted reading (
"12°C · Rain") when the response is ok - returns
"City not found"when it is not
The format helper is already written for you.
exercise.js
console · run your code or check the answer