| 21 | |
| 22 | // for any particular city |
| 23 | const getWeather=async (city)=> |
| 24 | { |
| 25 | try{ |
| 26 | const response= await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=dab3af44de7d24ae7ff86549334e45bd`, |
| 27 | |
| 28 | {mode: 'cors'} |
| 29 | ); |
| 30 | |
| 31 | const weatherData= await response.json(); |
| 32 | console.log(weatherData); |
| 33 | const{name}=weatherData; |
| 34 | const{feels_like}=weatherData.main; |
| 35 | const{id,main}=weatherData.weather[0]; |
| 36 | loc.textContent=name; |
| 37 | climate.textContent=main; |
| 38 | tempvalue.textContent=(feels_like-273).toFixed(2); |
| 39 | if(id<300 && id>200) |
| 40 | { |
| 41 | tempicon.src="../assets/Images/weather_icons/thunderstorm.svg" |
| 42 | } |
| 43 | else if(id<400 && id>300) |
| 44 | { |
| 45 | tempicon.src="../assets/Images/weather_icons/cloud-solid.svg" |
| 46 | } |
| 47 | else if(id<600&& id>500) |
| 48 | { |
| 49 | tempicon.src="../assets/Images/weather_icons/rain.svg" |
| 50 | } |
| 51 | else if(id<700 && id>600) |
| 52 | { |
| 53 | tempicon.src="../assets/Images/weather_icons/snow.svg" |
| 54 | } |
| 55 | else if(id<800 && id>700) |
| 56 | { |
| 57 | tempicon.src="../assets/Images/weather_icons/clouds.svg" |
| 58 | } |
| 59 | else if(id==800) |
| 60 | { |
| 61 | tempicon.src="../assets/Images/weather_icons/clouds-and-sun.svg" |
| 62 | } |
| 63 | |
| 64 | //Change Temperature to Celsius/Fahrenheit |
| 65 | temperatureSection.addEventListener('click' , ()=>{ |
| 66 | if(tempunit.textContent==="F"){ |
| 67 | tempunit.textContent = "°C"; |
| 68 | tempvalue.textContent=(feels_like-273).toFixed(2); |
| 69 | |
| 70 | } |
| 71 | else{ |
| 72 | tempunit.textContent = "F"; |
| 73 | tempvalue.textContent= |
| 74 | ((feels_like- 273.15) * 9/5+ 32).toFixed(2); |
| 75 | } |
| 76 | }); |
| 77 | |
| 78 | } |
| 79 | catch(error) |
| 80 | { |