Tags: #lesson
#level2
#javascript
#weather
Goal: Create a weather app using POJOs, helper functions, and Bootstrap cards.
Level: Beginner web development
Tools: VS Code, Git, web browser
π New! This lesson is now available in a step-by-step format. Click here to use the interactive lesson structure where each step has its own focused file.
π‘ Tip: Use these direct links to jump to any specific step or challenge you want to work on!
my-weather
What this accomplishes: Setting up a proper Git repository is the foundation of any development project. By creating a dedicated repository for your weather app, you establish version control, enable collaboration, and create a professional development environment. This step ensures your code is safely stored, tracked, and can be shared with others or deployed to the web.
π Need help setting up your repository? Check out our Start a New Project Guide for step-by-step instructions on creating repositories, cloning, and initial setup.
Create these files in your my-weather
repository:
File structure:
my-weather/
βββ index.html
βββ script.js
βββ helpers.js
βββ README.md
Include helpers.js and script.js in index.html:
<script src="helpers.js"></script>
<script src="script.js"></script>
What this accomplishes: Creating the proper file structure establishes the foundation for your weather app. The HTML file serves as the user interface, the JavaScript files contain the app logic and helper functions, and the README documents your project. This organization follows web development best practices and makes your code maintainable and easy to understand for other developers.
π Need help with helper functions? Check out our Helpers Guide for detailed instructions on downloading, setting up, and using the helper functions like
setText()
that youβll need for this project.
Option A: Copy from Lesson 2
current-weather.json
files from the my-search-params
repositoryOption B: Re-download
current-weather.json
What this accomplishes: Weather data is the core content that your app will display. By using real API data from previous lessons or downloading fresh data, you ensure your app works with authentic information rather than placeholder text. This step also reinforces the connection between different lessons and shows how data can be reused across projects.
Copy the JSON data into the top of your script.js
file to create a POJO:
// Weather data POJO
var newOrleansWeather = {
"latitude": 29.95,
"longitude": -90.07,
"current_weather": {
"temperature": 31.2,
"windspeed": 4.5,
"weathercode": 1
}
};
What this accomplishes: Converting JSON data into a JavaScript POJO (Plain Old JavaScript Object) makes the weather information easily accessible in your code. This structured approach allows you to reference specific weather properties like temperature and wind speed using dot notation, making your code more readable and maintainable than working with raw JSON strings.
In your index.html
, create a Bootstrap card for New Orleans weather:
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">New Orleans Weather</h5>
<p class="card-text">
Temperature: <span id="temp"></span>Β°C<br>
Wind Speed: <span id="wind"></span> km/h<br>
Weather Code: <span id="code"></span>
</p>
</div>
</div>
What this accomplishes: The Bootstrap card provides a professional, responsive container for displaying weather information. By using span elements with specific IDs, you create placeholders that JavaScript can target and update dynamically. This approach separates the structure (HTML) from the content (JavaScript), following modern web development principles and making your app look polished and professional.
π‘ Note: A
<span>
tag is an inline HTML element used to mark up a part of text. Itβs perfect for targeting specific text with JavaScript because you can give it anid
attribute and then use functions likesetText()
to update just that part of the content without affecting the rest of the paragraph. π Learn more about span tags
In your script.js
, use setText()
and the POJO to display the weather:
// Display weather data
setText("temp", newOrleansWeather.current_weather.temperature);
setText("wind", newOrleansWeather.current_weather.windspeed);
setText("code", newOrleansWeather.current_weather.weathercode);
What this accomplishes: This step brings your weather app to life by connecting the data (POJO) to the user interface (HTML). The setText()
function dynamically updates the span elements with real weather information, transforming your static HTML into a dynamic, data-driven application. This demonstrates the fundamental concept of DOM manipulation and shows how JavaScript bridges the gap between data and display.
Create HTML files and JavaScript files for the other cities from Week 6 Lesson 2:
Required cities:
For each city:
atlanta.html
, seattle.html
)atlanta-script.js
, seattle-script.js
)setText()
to populate the dataWhat this accomplishes: Expanding your weather app to multiple cities demonstrates how to scale a project and manage multiple files effectively. This step teaches you about project organization, code reuse, and how to maintain consistency across similar pages. It also shows how the skills you learned for one city can be applied systematically to create a comprehensive multi-city weather application.
Example file structure:
my-weather/
βββ index.html β New Orleans (main page)
βββ script.js β New Orleans weather logic
βββ atlanta.html β Atlanta weather
βββ atlanta-script.js β Atlanta weather logic
βββ seattle.html β Seattle weather
βββ seattle-script.js β Seattle weather logic
βββ [city].html β Your chosen city
βββ [city]-script.js β Your city's weather logic
βββ helpers.js β Shared helper functions
βββ README.md
Work in your script.js
and index.html
files:
Challenge: Figure out how to implement this yourself! Youβll need to:
onEvent()
to listen for button clicksWhat youβll review:
onEvent()
π Learning Point: What is an Event Listener?
An event listener is a function that waits for a specific action (like a button click) to happen, then runs code in response. Think of it like setting up a security camera that watches for movement - when it detects something, it triggers an alarm.
In our code,
onEvent("show-weather", "click", function() { ... })
means:
- Watch the element with
id="show-weather"
- Wait for a βclickβ action
- Run this function when it happens
This is how we make websites interactive instead of just static displays!
Work in the same index.html
and script.js
files:
Important: Donβt create a second card! Use the same card HTML and just change the data when different buttons are clicked.
Challenge: Implement this yourself! Youβll need to:
onEvent()
listenerWhat youβll learn:
Extend your index.html
and script.js
files:
Challenge: Scale this up! Youβll need to:
onEvent()
listeners for each new buttonWhat youβll learn:
Youβve built:
onEvent()
Next: Use Postmanβs Code Generation to turn API requests into JavaScript fetch()
calls.