-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C22- Elzara and Priyanka- weather-report #2
base: main
Are you sure you want to change the base?
Conversation
…dOneTemp and reduceOneTemp with click.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work on weather-report, team!
const tempDisplay = document.getElementById('tempValue'); | ||
const increaseTemp = document.getElementById('increaseTempControl'); | ||
const decreaseTemp = document.getElementById('decreaseTempControl'); | ||
const landscape = document.getElementById('landscape'); | ||
const cityNameDisplay = document.getElementById('headerCityName'); | ||
const cityNameInput = document.getElementById('cityNameInput') | ||
const tempButton = document.getElementById('currentTempButton'); | ||
const skySelect = document.getElementById('skySelect'); | ||
const gardenContent = document.getElementById('gardenContent'); | ||
const skyDisplay = document.getElementById('sky'); | ||
const resetButton = document.getElementById('cityNameReset'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that you organized all these variables related to page elements in one place. It makes your file more organized which ultimately makes your codebase more maintainable.
For variable names, it's more descriptive and would make your code more self-documenting to include the name of the element type where possible (which you do on line 11).
Suggestions for lines 2-3.
const increaseTempButton = document.getElementById('increaseTempControl');
const decreaseTempButton = document.getElementById('decreaseTempControl');
const updateTempColor = () => { | ||
tempDisplay.classList.remove('red', 'orange', 'yellow', 'green', 'teal'); | ||
|
||
if (temperature >= 80) { | ||
tempDisplay.classList.add('red'); | ||
} else if (temperature >= 70) { | ||
tempDisplay.classList.add('orange'); | ||
} else if (temperature >= 60) { | ||
tempDisplay.classList.add('yellow'); | ||
} else if (temperature >= 50) { | ||
tempDisplay.classList.add('green'); | ||
} else { | ||
tempDisplay.classList.add('teal'); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be mindful of indentation
const updateTempColor = () => { | |
tempDisplay.classList.remove('red', 'orange', 'yellow', 'green', 'teal'); | |
if (temperature >= 80) { | |
tempDisplay.classList.add('red'); | |
} else if (temperature >= 70) { | |
tempDisplay.classList.add('orange'); | |
} else if (temperature >= 60) { | |
tempDisplay.classList.add('yellow'); | |
} else if (temperature >= 50) { | |
tempDisplay.classList.add('green'); | |
} else { | |
tempDisplay.classList.add('teal'); | |
} | |
}; | |
const updateTempColor = () => { | |
tempDisplay.classList.remove('red', 'orange', 'yellow', 'green', 'teal'); | |
if (temperature >= 80) { | |
tempDisplay.classList.add('red'); | |
} else if (temperature >= 70) { | |
tempDisplay.classList.add('orange'); | |
} else if (temperature >= 60) { | |
tempDisplay.classList.add('yellow'); | |
} else if (temperature >= 50) { | |
tempDisplay.classList.add('green'); | |
} else { | |
tempDisplay.classList.add('teal'); | |
} | |
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using if/else if/else which can be brittle and bug-prone, consider using a list to store the landscape emojis. Then you can write logic that compares the first element in each inner list against temperature
and grab the color or emojis.
temperatureColorsAndLandscapes = [
[80, "red", "🌵__🐍_🦂_🌵🌵__🐍_🏜_🦂"],
[70, "orange", "🌸🌿🌼__🌷🌻🌿_☘️🌱_🌻🌷"],
[60, "yellow", "🌾🌾_🍃_🪨__🛤_🌾🌾🌾_🍃"],
[50, "green", "🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲"],
[40, "teal", "🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲"],
]
if (temperature >= 80) { | ||
landscapeDisplay = '🌵__🐍_🦂_🌵🌵__🐍_🏜_🦂'; | ||
} else if (temperature >= 70) { | ||
landscapeDisplay = '🌸🌿🌼__🌷🌻🌿_☘️🌱_🌻🌷'; | ||
} else if (temperature >= 60) { | ||
landscapeDisplay = '🌾🌾_🍃_🪨__🛤_🌾🌾🌾_🍃'; | ||
} else { | ||
landscapeDisplay = '🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above about incorporating a data structure to iterate over to get values instead of needing conditional logic.
const addOneTemp = () => { | ||
temperature += 1; | ||
tempDisplay.innerText = temperature; | ||
|
||
updateDisplay() | ||
}; | ||
|
||
const reduceOneTemp = () => { | ||
temperature -= 1; | ||
tempDisplay.innerText = temperature; | ||
|
||
updateDisplay() | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice that the difference between these functions is +=
and -=
.
Consider combining this logic into one helper function handleTempChange
that could be registered with both the ⬆️ and ⬇️ buttons.
const handleTempChange = (event) => {
if (event.target.id === 'increaseTempControl') {
temperature ++;
} else {
temperature--;
}
updateDisplay();
};
increaseTemp.addEventListener('click', addOneTemp); | ||
decreaseTemp.addEventListener('click', reduceOneTemp); | ||
|
||
const updateCityName = () => { | ||
const cityText = cityNameInput.value; | ||
cityNameDisplay.innerText = cityText; | ||
}; | ||
|
||
cityNameInput.addEventListener('input', updateCityName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that you put all this callback logic into helper functions and then you register them with an event by function name (instead of just passing in a long anonymous function which can be hard to read and debug).
One thing that would help make this file more organized is to have all your addEventListener
function calls in one place. As the code is written right now, these calls are sprinkled throughout and one has to look for them instead of just going to one place and know they're all there.
I'd recommend creating a helper called like registerEventHandlers
and it could be called on page load
const registerEventHandlers () => {
increaseTemp.addEventListener('click', addOneTemp);
decreaseTemp.addEventListener('click', reduceOneTemp);
cityNameInput.addEventListener('input', updateCityName);
// etc etc
};
document.addEventListener('DOMContentLoaded', () => {
registerEventHandlers();
};
skyOptions.forEach(option => { | ||
const optionElement = document.createElement('option'); | ||
optionElement.value = option.name.toLowerCase(); | ||
optionElement.textContent = option.name; | ||
skySelect.appendChild(optionElement); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer this to be in a function and call the function where needed
tempButton.addEventListener('click', getCurrentTemp); | ||
|
||
// sky options with corresponding clouds | ||
const skyOptions = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constant variables should be named with capital letters to convey that the values should not be changed.
const skyOptions = [ | |
const SKY_OPTIONS = [ |
const temperatureKelvin = weatherData.main.temp; | ||
temperature = Math.round((temperatureKelvin - 273.15) * 9/5 + 32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer this logic to be in a helper function that gets called here. Invoking a descriptively named helper function would make the logic here more readable and also be concerned with doing one less thing.
const convertTemp = (temperatureKelvin) => {
return Math.round((temperatureKelvin - 273.15) * 9/5 + 32);
};
return axios | ||
.get(`http://localhost:5000/weather?lat=${lat}&lon=${lon}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer url string be referenced by a constant variable
return axios | |
.get(`http://localhost:5000/weather?lat=${lat}&lon=${lon}`) | |
const baseURL = 'http://localhost:5000'; | |
return axios | |
.get(`${baseURL}/weather?lat=${lat}&lon=${lon}`) |
// function that gets location | ||
const getLocation = (cityName) => { | ||
return axios | ||
.get(`http://localhost:5000/location?q=${cityName}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above about constant var for base url
No description provided.