-
Notifications
You must be signed in to change notification settings - Fork 85
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
Kunzite: Angelica and Alyssa #68
base: main
Are you sure you want to change the base?
Conversation
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.
Good job, Angelica and Alyssa. I've left some feedback on how you can refactor your JavaScript in order to make it more organized.
Things to consider:
- When assigning variables, don't forget your semicolon, even if the value is a function.
- Collect all your
addEventListener('DOMContentLoaded')
calls into one function in order to dry up repetitive code.
@@ -0,0 +1,156 @@ | |||
'use strict' |
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.
'use strict' | |
'use strict'; |
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.
Looks like there are several elements scattered around the file, along with some repetitive code for registering event listeners individually.
Let's streamline this by creating one function that will contain any elements that need an event listener:
const registerEventHandler = () => {
const skyOptions = document.getElementById('skySelect');
skyOptions.addEventListener('change', changeSky);
const decreaseTempButton = document.getElementById('decreaseTempControl');
decreaseTempButton.addEventListener('click', decreaseTemp);
// or: decreaseTempHandler(); since you have a function already built for this
// so on and so forth
};
// then, likely placed at the bottom of the file
document.addEventListener('DOMContentLoaded', registerEventHandler);
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.
The rest of the variables that grab the values we wish to calculate or re-render, let's collect them together for the sake of organization, or collect them all into one object of key-value pairs.
Just like the event and event listeners we registered in registerEventHandler
, we can create an object of initial state:
const state = {
tempValue: 0,
currentCity: 'Seattle',
landscape: null
};
// then inside the registerEventHandler
state.tempValue = parseInt(document.getElementById('tempValue').textContent);
state.landscape = document.getElementById('landscape');
// so on and so forth
This isn't necessary, but it would create some structure and organization for other coders when reading your code.
const colorTemp = () => { | ||
if (tempValue >= 80) { | ||
tempElement.style.color = 'red'; | ||
} else if (tempValue >= 70) { | ||
tempElement.style.color = 'coral'; | ||
} else if (tempValue >= 60) { | ||
tempElement.style.color = 'orange'; | ||
} else if (tempValue >= 50) { | ||
tempElement.style.color = 'green'; | ||
} else { | ||
tempElement.style.color = 'blue'; | ||
}; | ||
} | ||
|
||
const addLandscape = () => { | ||
if (tempValue >= 80) { | ||
landscapeElement.textContent = '🌵__🐍_🦂_🌵🌵__🐍_🏜_🦂'; | ||
} else if (tempValue >= 70) { | ||
landscapeElement.textContent = '🌸🌿🌼__🌷🌻🌿_☘️🌱_🌻🌷'; | ||
} else if (tempValue >= 60) { | ||
landscapeElement.textContent = '🌾🌾_🍃_🪨__🛤_🌾🌾🌾_🍃'; | ||
} else { | ||
landscapeElement.textContent = '🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲'; | ||
}; | ||
} |
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.
Another way to format these two methods (maybe combine them into one) would be to have an object that has temp values as keys, and then you could have values be a list. The list could hold background color and background image, like:
const tempDisplayObject = {
80: ['red', "🌵__🐍_🦂_🌵🌵__🐍_🏜_🦂"],
70: ['orange', "🌸🌿🌼__🌷🌻🌿_☘️🌱_🌻🌷"]
}
When you have an object, you don't need to do if/else if/else if/ else which can introduce bugs and also adds complexity to the method. If you have an object, than you can iterate over the object and check the keys with state.tempValue
and then get the correct values to set the color and landscape emojis.
const getCurrentTemp = () => { | ||
locationCall() | ||
.then((temp) => { | ||
tempElement.textContent = parseInt(temp); | ||
tempValue = parseInt(temp); | ||
}) | ||
.catch((error) => { | ||
console.log('The value of error is:', error); | ||
}); | ||
} |
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 job separating out the axios
promises into individual functions!
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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.
} else { | ||
tempElement.style.color = 'blue'; | ||
}; | ||
} |
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.
} | |
}; |
} else { | ||
landscapeElement.textContent = '🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲'; | ||
}; | ||
} |
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.
} | |
}; |
tempElement.textContent = tempValue; | ||
colorTemp(); | ||
addLandscape(); | ||
} |
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.
} | |
}; |
All assignments should have a semicolon at the end
|
||
const updateCityName = () => { | ||
cityNameElement.textContent = cityNameInput.value; | ||
locationCall() |
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.
Does the promise this function returns need a then
and catch
here?
No description provided.