Amethyst: Megan & Jay - #61
Conversation
| const state ={ | ||
| temp: 0, | ||
| number: document.getElementById("temperature-now"), | ||
| } |
There was a problem hiding this comment.
Love how you all chose to use this psuedostate! I think that it is great practice for what you will see in React though it will be utilized differently!
| const upButton = document.getElementById("up-button"); | ||
| const downButton = document.getElementById("button-down"); | ||
| const tempButton = document.getElementById("get-temp") | ||
| // let number = document.getElementById("temperature-now"); | ||
| // let temp = parseFloat(number.innerText); | ||
| state.temp = parseFloat(state.number.innerText); | ||
| const weatherGarden = document.getElementById('weather-garden'); | ||
| const weatherEmojis = document.getElementById('weather-emojis'); | ||
| const cityDisplay = document.getElementById("city-display") | ||
| const cityId = document.getElementById("city-input") | ||
| const skyOptions = document.getElementById('change-sky'); | ||
| const skyEmojis = document.getElementById('chosen-sky-emojis'); | ||
| const resetBtn = document.getElementById('resetbtn'); |
There was a problem hiding this comment.
Pieces of code like this are usually found in an event handler that runs on the event DOMContentLoaded, basically once the page is html is loaded on the webpage you will run a bunch of initial logic such as grabbing elements and adding event handlers to them. It will look something like this:
document.addEventListener("DOMContentLoaded", function () {
const increaseTemp = document.querySelector("#increase-temp");
const decreaseTemp = document.querySelector("#decrease-temp");
const displayTemp = document.querySelector("#display-temp");
const resetButton = document.querySelector("#reset-button");
const searchButton = document.querySelector("#search-button");
const cityName = document.getElementById("city-name");
const cityInput = document.getElementById("city-input");
const selectSky = document.querySelector("#sky-dropdown");
const result = document.querySelector("#sky");
const landscape = document.querySelector("#landscape");
increaseTemp.addEventListener("click", increaseTemperature);
decreaseTemp.addEventListener("click", decreaseTemperature);
...
}This is also usually found at the bottom of the file too, that way you can put all your function definitions for event handlers above it!
| const getLanLon = (city) => { | ||
| return axios.get(`http://localhost:5000/location?q=${city}`) | ||
| .then(response => { | ||
| let lat = response.data[0].lat | ||
| let lon = response.data[0].lon | ||
| console.log(response.data[0].display_name) | ||
| return [lat, lon] | ||
| }) | ||
| .catch(error => { | ||
| console.log(error) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Here's an alternative to promise chaining! You can use the async/await keywords to have your function wait until a promise is fulfilled and then you pass the results of that promise to other pieces of your function like assigning your return values as variables or other things like that. It looks something like this:
const findCityLocation = async () => {
let lat;
let lon;
const resp = await axios.get('http://127.0.0.1:5000/location', {
params: {
q: document.getElementById("city").value
}
})
try {
[lat, lon] = resp.data[0]
getWeather({ lat: lat, lon: lon});
} catch (error) {
console.log(error)
}
};| resetBtn.addEventListener("click",() =>{ | ||
| cityDisplay.innerText = ""; | ||
| cityId.value = "" | ||
| } ) No newline at end of file |
There was a problem hiding this comment.
Awesome job everyone! You really ate this one up! Please feel free to reach out to me if you have any questions about the comments I left or if you want to discuss anything in greater detail! ⭐️
No description provided.