Ria Choi
← Portfolio

Weather App

Weather App

Weather App (freeCodeCamp Project)

I built this weather app with HTML, CSS, and vanilla JavaScript as part of the freeCodeCamp curriculum. Users pick a city from a dropdown, click a button, and the app fetches live weather data from freeCodeCamp's Weather Proxy API and displays the temperature, humidity, wind, and conditions with a weather icon.

Fetching data with async/await

I wrote a function that calls the API, checks that the response succeeded, and returns the JSON. If anything fails, it returns undefined instead of crashing:

JavaScript
async function getWeather(city) {
  try {
    const url = `https://weather-proxy.freecodecamp.rocks/api/city/${city}`;
    const response = await fetch(url);

    if (!response.ok) throw new Error("API error");

    const data = await response.json();
    return data;
  } catch (error) {
    console.error(error);
    return undefined;
  }
}

Updating the page and handling errors

showWeather waits for the data, shows an alert if the request failed, and otherwise updates each element on the page. I used optional chaining (?.) so a missing field doesn't throw, and a small helper that shows "N/A" for missing values:

JavaScript
function safe(value) {
  return value === undefined ? "N/A" : value;
}

async function showWeather(city) {
  const data = await getWeather(city);

  if (!data) {
    alert("Something went wrong, please try again later.");
    return;
  }

  weatherIcon.src = safe(data.weather?.[0]?.icon);
  mainTemperature.textContent = safe(data.main?.temp);
  windGust.textContent = safe(data.wind?.gust);
  locationEl.textContent = safe(data.name);
}

Connecting the UI

The dropdown holds the city names, and the button triggers the fetch. If no city is selected, nothing happens:

JavaScript
getWeatherBtn.addEventListener("click", () => {
  const city = citySelect.value;

  if (!city) return;
  showWeather(city);
});

What I learned

  • Calling an API with fetch and async/await
  • Handling errors with try/catch and checking response.ok
  • Reading nested JSON safely with optional chaining
  • Updating the DOM from API data and responding to user events