DEV Community


FreeCodeCamp take home project 1 walkthrough - weather app

agbanusi profile image Agbanusi John Updated on ・3 min read

Okay, I'll be writing a blog series talking about all of freecodecamp take home projects and more, this will be the first blog post and will cover the "show the weather app".

The project is simply a weather app that shows the weather in your location, also has features like an icon to describe the weather and the temperature shown that can be toggled between the celsius and fahrenheit unit.

Project: Weather App
Approximate Time: 2hrs
Difficulty level: Easy
The link to the challenge is here.

I used React for this project which I must say was an overkill, normal HTML, CSS and JS is enough.
First, we have to get the location of our user, for that we'll use navigator.geolocation which will give a prompt to allow or block this site from knowing your current location.

The code could look like:

if (navigator.geolocation){
 var lat=position.coords.latitude;
 var long=position.coords.longitude;

FreeCodeCamp helped us by providing an api we can make a get request to, querying with longitude and latitude we're going to fetch and use the json response we receive as below. But before the code, I put the rest of the code including the fetch request inside the if statement above to ensure we're sending the right kind of request to the api.

  //function goes here

The code above requests from the api, with queries "lon" for longitude and "lat" for latitude.

We should know how the response look like right?

{"coord":{"lon":139,"lat":35},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":""}],"base":"stations","main":{"temp":25.56,"feels_like":28.46,"temp_min":25.56,"temp_max":25.56,"pressure":1006,"humidity":67},"wind":{"speed":0.45,"deg":116,"gust":0.89},"clouds":{"all":59},"dt":1593399515,"sys":{"type":3,"id":2019346,"country":"JP","sunrise":1593372765,"sunset":1593424915},"timezone":32400,"id":1851632,"name":"Shuzenji","cod":200}

From these results above, our main target data is the data.main.temp for the temperature,[0].icon for the icon to be displayed, for displaying country. Note the data is from the fetch function above. For ease and simplicity, we put the entire React component below in it.

So, what's next? Designing the app and displaying the required data after getting from the api.

The React component could look like this:

class Main extends React.Component {


      return temp+'°C'
      return +((9*temp/5)+32).toFixed(2) +'°F'
  render() {
      return (
        <h1>A simple Weather App</h1>
        <div className='show'>
         <button onClick={this.toggle}> Change unit </button>
        <img src={[0].icon} />
ReactDOM.render(<Main />, document.getElementById('root'))

Breaking it down, we have two functions and one state variable here. The state variable is for toggling between celsius or fahrenheit, so on the click of a button, we could change the temperature displayed. This toggling is taken care of by the toggle function, which then triggers a change in state and also the convert temperature function since the button click will trigger a re-render.

As the last line shows we have to render to an element with id of 'root', so our html code should look like this:

        <link rel="stylesheet" href="style.css">
        <div id="root"></div>
        <script src="index.js"></script>

The main focus is that you have an element, div in this case with id 'root', so that React can render your result to that element. You can then design as you deem fit, you can check out mine here on codepen.

Discussion (0)

Editor guide