Used the openweather api

so this my third attempt at using api after doing the pokemon api project and i want feedback on what i can do better on

i was getting confused a little on just getting the data in the console but i got it. I was wondering how i could improve make my js more organized and need confirm if my kelvin to f function is correct

also i want to hide my api key how would i go about that

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>weather map</title>
    <link rel="stylesheet" href="styles.css"/>
</head>
<body>
    <h1>Todays weather</h1>
    <h4>type in a us zip code</h5>
    <input type="number" id='input1' placeholder="type in zip code">
    
   
    <button type="button" id="search">Search</button>
    <div id="info">
 <div id="temps"></div>
 <div id="feeltemp"></div>



    </div>
 <script src="script.js"></script>   
</body>
</html>
const input1 = document.getElementById("input1");
const button = document.getElementById("search");
const info =  document.getElementById("info")
const key = '23862e68da6c3fc6f6001c4e91ae5026';
const temp = document.getElementById("temps");
const feeltemp = document.getElementById("feeltemp");
 const  f = (input) =>{
    return Math.floor((input -273.15) *9/5 + 32);
 }
  console.log(f(298.706))
const fetchData = async (input1) =>{
    const err ="no data";
    try{
    const res = await fetch(`https://api.openweathermap.org/data/2.5/weather?zip=${input1},us&appid=${key}`);
    const data = await res.json();
    const { weather, main} = data;
    temp.innerHTML = `The Temperature is ${f(main.temp)}f`;
    console.log(weather[0]);
    console.log(data)
    }catch(err){
 console.log(err);
    }
}

console.log(1+1);
button.addEventListener('click',() =>{
    fetchData(input1.value)
});

body{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
#input1, #input2{
    width: 5%;
}
button{
    width: 5%;
}
#info{
    border: 1px solid black;
    width: 40%;
}

Hi @GavinEscanilla

You could hide the api key in an environment variable file. You’ll need to search for documentation on how to do that.

Use the fetch call for several zip codes and check the temp and your conversion using a kelvin to fahrenheit calculator.

https://api.openweathermap.org/data/2.5/weather?zip=10001,us&appid=removed

image

I changed some of the html and css to simply a few things.
image

Since the input is a five digit code, how about setting minimum and maximum values?

Some tweaking of the js.
image

If you are not using the feeltemp variable maybe remove that code.
Once the api is working, remove the console logs.

The background looks a little bland.
Consider associating certain colours with certain temperature ranges. Reds for warm, blues for cool temperatures.

Happy coding

1 Like