A Beginner's Journey into Building a Weather App

migsldev

migsldev

Posted on February 7, 2024

A Beginner's Journey into Building a Weather App

A Beginner's Journey into Building a Weather App

Hey there! I'm thrilled to share my adventure of creating a Weather App, especially for those who, like me, are just stepping into the world of software development. Picture this: a rookie developer armed with basic HTML, CSS, and JavaScript skills, diving headfirst into the exciting realm of Single Page Applications (SPAs).

Getting Started:

Living in Melbourne, Australia (where Four seasons can happen in a day), the idea popped into my head: why not build an app that lets users check the weather conditions in different locations? A common first time project but simple, yet practical, right? With a bit of research, I stumbled upon the OpenWeatherMap API, a goldmine for weather data enthusiasts like me. Best part? No need for an API key for basic functionality - perfect for a newbie like me!

Setting Sail:

With a spark of enthusiasm, I began crafting my Weather App. Armed with my trusty text editor and a hefty dose of determination, I started laying down the groundwork. Let me share a peek into the heart of my Weather App - the JavaScript code that powers it:

    // Current Weather
    fetch(currentWeatherUrl)
        .then(response => response.json())
        .then(data => 
            { displayWeather(data);
            })
            .catch(error => {
                console.error('Error fetching current weather data:', error);
                alert('Error fetching current weather data. Please try again.');
            });

    // Hourly weather
    fetch(forecastUrl)
        .then(response => response.json())
        .then(data => 
            { displayHourlyForecast(data.list);
            })
            .catch(error => {
                console.error('Error fetching current weather data:', error);
                alert('Error fetching current weather data. Please try again.');
            });
}
Enter fullscreen mode Exit fullscreen mode

Navigating Stormy Waters:

Ah, but the journey was not without its challenges! As an entry-level developer, grappling with asynchronous JavaScript and event-driven programming was like navigating stormy waters. But fear not, for with persistence and a dash of Googling, I weathered the storms and emerged stronger than ever!

Hoisting the Sails:

One aspect of JavaScript that proved particularly useful in my Weather App SPA was asynchronous programming. By leveraging the fetch API, I was able to asynchronously fetch weather data from the OpenWeatherMap API and update the UI dynamically based on the received data. Here's a snippet of the asynchronous fetch function in action:

// Fetching current weather data
fetch(currentWeatherUrl)
    .then(response => response.json())
    .then(data => {
        displayWeather(data);
    })
    .catch(error => {
        console.error('Error fetching current weather data:', error);
        alert('Error fetching current weather data. Please try again.');
    });
Enter fullscreen mode Exit fullscreen mode

A Beginner-Friendly Explanation:

  1. Fetching Data: We're using JavaScript to ask a website (specifically, the OpenWeatherMap API) for some information, like the current weather in a city.

  2. Promise: We're using something called a "Promise" to handle the request. Think of a Promise like sending a message and waiting for a reply. The fetch function returns a Promise, which represents the request we've sent.

  3. Response Handling: Once we get a reply (a response) from the website, we want to read the information it sent back. We use .then() to say "When you get a reply, do this:". Inside .then(), we have a function that converts the response into a format we can easily work with (JSON format).

  4. Using the Data: After we've converted the response into something useful, we want to do something with it. We use another .then() to say "Now that we have the data, do this:". Inside this .then(), we call a function (displayWeather) to show the weather information on our app.

  5. Handling Errors: Sometimes things can go wrong, like if the website is down or there's a problem with our internet connection. We use .catch() to say "If something goes wrong, do this:". Inside .catch(), we handle the error by logging it to the console (so we can see what went wrong) and showing an alert to the user.

Looking at the Horizon:

Looking back on my journey of learning the fundamentals of JavaScript, I can't help but marvel at how far I've come.

From simple variables and loops to complex concepts like asynchronous programming, each step of the journey has been a lesson in growth and perseverance.

Truth be told, like any aspiring software developer, there were a "few" hiccups along the way such as getting stuck with some of the labs testing, skipping some exercises then instantly regretting as it is very related to the next topic, taking a long time to grasp the logic behind how the function works and a lot more!

There were times during the Phase when I struggled with a set of exercises (I remember like it was yesterday - Debugging) and I'd tell myself "Can I actually understand and solve this?" despite checking Google and Stack Overflow wanting to give up. However, like any voyage, sometimes a sign or a star (solution or "light-bulb" moment") kicks in that then reignites the desire to learn more!

Through this adventure, I learned invaluable lessons about the importance of resilience, patience, and continuous learning. Each bug squashed and each feature implemented brought me one step closer to becoming a proficient developer. And let's not forget the satisfaction of seeing my Weather App come to life - a testament to my newfound skills and determination!

What Lies Ahead?

As I finalize this project, I do so with a sense of pride and accomplishment. But the journey doesn't end here. With newfound confidence and a hunger for knowledge, I set sail towards new horizons, ready to tackle whatever challenges come my way.

So, fellow beginner programmers, fear not the unknown! Embrace the challenges, cherish the victories, and never stop learning. Who knows what treasures await on the horizon? Until next time, happy coding!


Weather-App-SPA Project: GitHub Repository

💖 💪 🙅 🚩
migsldev
migsldev

Posted on February 7, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How I Made A Gradient Generator
javascript How I Made A Gradient Generator

January 13, 2018