Vanilla javascript GeoLocation weather app

Just did a simple GeoLocation weather app using pure vanilla javascript. Do feedback :smile: esp the javascript coding part ^^

Your app its working ok for me. It reports the right location and temp. You should only change the button text because you don’t “Change Temperature” but you change unit measure from C to F and viceversa.

Would you mind posting a link to your github repository to make it a little easier to review your code?


yeah sure thanks :smiley:

Your project code looks fine. I really like the way you organized the code.

I made some small “refinements” (see below) which you may or may not wish to incorporate. Some are simplifications and others are just to make the code more readable (less use of this.something).


const getWeather = () =>{
      .then(({coords}) => coords)
      .then(position => weather.getWeather(position))
      .then(weather =>  user.paint(weather))
const weather = new Weather;
const coordinate = new GeoLocation;
const user = new UI;
document.addEventListener("DOMContentLoaded", getWeather)


// gets location from user browser

class Weather {
    constructor() {
        this.key = "03726b04c772c54015bff90e597deaf9";

    async getWeather({latitude,longitude}) {
        const endPoint = ``;
        const apiUrl = `${endPoint}lat=${latitude}&lon=${longitude}&APPID=${this.key}&units=metric`
        const response = await fetch(apiUrl);
        if(response.status === 200 ){
            return  await response.json();
        throw new Error("Unable to fetch data")

The paint method in your UI class.

        sys:{country: countryName},
        weather:[{main, icon: iconName}]
    }) {
        const {country, weather, icon,  temperature, button, convertF} = this;
        country.textContent = `${name},${countryName}`;
        weather.textContent =`${main}`;
        temperature.textContent = `${temp} C`;

        button.addEventListener("click", () => {
            const currContent = temperature.textContent;
            temperature.textContent = currContent === `${temp} C`
                ? `${convertF(temp)} F`
                :  `${temp} C`