This project was created to gain familiarity with JavaScript, DOM manipulation, and API integration. It fetches weather data from the Open-Meteo API and displays current weather conditions and a 5-day forecast.
- Search for locations by postal code.
- View current weather conditions and a 5-day forecast.
- Toggle between Celsius and Fahrenheit units.
- HTML
- Bootstrap
- JavaScript
- Open-Meteo API
- Enhance the UI with better styling and responsive design
- Add more robust error handling
- Purpose: Maps weather codes from the Open-Meteo API to human-readable weather descriptions.
- Example Usage: Used to display weather descriptions in the forecast and current conditions.
- Example Entry:
[0, "Clear Skies"], [61, "Light Rain"], [95, "Slight or Moderate Thunderstorm"]
- Purpose: Maps weather codes to corresponding weather icons (using Iconify icons).
- Example Usage: Used to display weather icons in the forecast and current conditions.
- Example Entry:
[0, "wi:day-sunny"], [61, "mdi:weather-rainy"], [95, "mdi:weather-lightning"]
- Purpose: Maps temperature units (
fahrenheitorcelsius) to their respective symbols (°For°C). - Example Usage: Used to display the correct temperature unit in the forecast and current conditions.
- Example Entry:
["fahrenheit", "°F"], ["celsius", "°C"]
- Purpose: Represents a location with properties such as postal code, latitude, longitude, and forecast data.
- Properties:
postalCode: The postal code of the location.admin1: The state or region of the location.admin2: The city or municipality of the location.coordinates: An instance of theCoordinatesclass containing latitude and longitude.forecast: The forecast data for the location.
- Methods:
setForecast(forecast): Sets the forecast data for the location.setCurrentConditions(currentConditions): Sets the current weather conditions for the location.
- Purpose: Represents the latitude and longitude of a location.
- Properties:
latitude: The latitude of the location.longitude: The longitude of the location.
- Purpose: Represents the forecast data for a location.
- Properties:
data: The raw forecast data returned by the Open-Meteo API.
- Purpose: Represents the current weather conditions for a location.
- Properties:
temperature: The current temperature.winddirection: The wind direction in degrees.windCardinalDirection: The wind direction as a cardinal direction (e.g., N, NE, E).windspeed: The wind speed.weathercode: The weather code for the current conditions.is_day: Indicates whether it is daytime.
- Methods:
windDirectionConversion(): Converts the wind direction in degrees to a cardinal direction.
- Purpose: Validates whether a given postal code is a valid 5-digit number.
- Parameters:
postalCode(string): The postal code to validate.
- Returns:
trueif valid,falseotherwise.
- Purpose: Constructs the URL for the Open-Meteo geocoding API based on the postal code.
- Parameters:
postalCode(string): The postal code to search for.
- Returns: A URL string.
- Purpose: Fetches location suggestions based on the user's query.
- Parameters:
query(string): The search query (e.g., postal code or location name).
- Returns: A promise that resolves to an array of location suggestions.
- Purpose: Dynamically updates the DOM with location suggestions.
- Parameters:
suggestions(array): An array of location objects returned by the geocoding API.
- Purpose: Fetches geolocation data for a given postal code.
- Parameters:
geolocationSearchURL(string): The URL for the geocoding API.
- Returns: A promise that resolves to the geolocation data.
- Purpose: Fetches forecast data for a given location.
- Parameters:
forecastURL(string): The URL for the forecast API.
- Returns: A promise that resolves to a
Forecastobject.
- Purpose: Constructs the URL for the Open-Meteo forecast API based on the coordinates.
- Parameters:
myCoordinates(Coordinates): An instance of theCoordinatesclass.
- Returns: A URL string.
- Purpose: Handles the search functionality, fetching geolocation and weather data, and updating the DOM.
- Steps:
- Validates the postal code.
- Fetches geolocation data.
- Fetches forecast data.
- Updates the DOM with current weather and forecast information.
- Purpose: Initializes the app when the DOM is fully loaded.
- Key Listeners:
- Input Suggestions: Listens for user input in the search bar and fetches location suggestions.
- Click Outside Suggestions: Clears the suggestions list when the user clicks outside.
- Search Button: Triggers the
handleSearchfunction when the search button is clicked. - Enter Key: Triggers the
handleSearchfunction when the Enter key is pressed. - Unit Toggle: Toggles between Celsius and Fahrenheit and updates the forecast.



