hi. i been struggle about this api things few days and i can’t understand much even tho i search about it. can some one please explant about the code block. English is not my first languages so… please if you can explain very simple. Also i try to get API from the openweathermap.org. I know there is something wrong about the last code block but i can’t help to resolve. Thank you.
const apiKey ="xxxxxxxxxxxxxxxxxxxxxxxxx";
const url = "https://api.openweathermap.org/data/2.5/weather?q=";
const queryParams ="&appid="
//get elements
const inputValue = document.getElementsByClassName('input');
const submitButton = document.getElementsByClassName('btn'),
cityName = document.getElementById("city-name"),
icon = document.getElementById("icon"),
temperature = document.getElementById("temp"),
humidity = document.getElementById("humidity-div");
// Place to put the input value
function enterPressed(event) {
if (event.key === "Enter"){
findWeatherDetails();
}
}
//The press button function
function findWeatherDetails(){
if (inputValue.value === ""){
} else {
let searchLink = url + inputValue.value + queryParams + apiKey;
getWeather(searchLink, theResponse);
}
}
function theResponse(response){
let jsonObject = JSON.parse(response);
cityName.innerHTML = jsonObject.name;
icon.src = "https://openweathermap.org/img/w/" + jsonObject.weather[0].icon + "png";
temperature.innerHTML = searchLink + "&units=metric";
humidity.innerHTML = jsonObject.main.humidity + "%";
}
// ABOUT THIS CODE. PLEASE HELP
async function getWeather(searchLink, callback) {
try{
const response = await fetch(searchLink, {cache: 'no-cache'})
if(response.ok){
const jsonResponse = await response.json();
callback(response,jsonResponse);
}
} catch(error){
console.log(error);
}
};