Skip to content

getting rid of global state#1

Open
Smelbows wants to merge 1 commit into
masterfrom
sarahs-refactoring-of-functions
Open

getting rid of global state#1
Smelbows wants to merge 1 commit into
masterfrom
sarahs-refactoring-of-functions

Conversation

@Smelbows

Copy link
Copy Markdown
Collaborator

Hey everyone! so I was asking Steve questions about refactoring (because apparently it's my favourite thing now) and I changed a few things on my local copy just to see what was possible. Then I thought, hey!!, we haven't done any branching or looking at pull requests yet, so why not! The changes don't affect how our site looks or runs in any way at all, so we can actually just ignore it if we want to, but if you'd like to we can look at the Javascript and pull it into the master. Essentially it takes away things that are in the global scope, and it uses just one function to get the weather icons for both the five day forecast and the main forecast. So the functions hopefully look simpler and easier to read without actually doing anything different. Btw, now I kind of want one of you to create another branch with some different changes just so that we have some merge conflicts to deal with (not really!!)

@netlify

netlify Bot commented Sep 22, 2021

Copy link
Copy Markdown

✔️ Deploy Preview for hardcore-fermat-e724bc ready!

🔨 Explore the source changes: d736ef0

🔍 Inspect the deploy log: https://app.netlify.com/sites/hardcore-fermat-e724bc/deploys/614b9303fa312e00075daa2f

😎 Browse the preview: https://deploy-preview-1--hardcore-fermat-e724bc.netlify.app

@Smelbows Smelbows left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put some comments so I could remember what I did!

Comment thread code/script.js
};

//Creates a weather icon based on the forecasted weather
function chooseWeatherIcon(filteredForecast, i) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function didn't actually need two arguments, so we've just given it each day's forecast (from 'filteredForcast[i]) instead

Comment thread code/script.js
let main = forecast.weather[0].main;
if (main === "Clouds") {
weatherImg = cloudyIcon;
return cloudyIcon;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function now returns an icon, which we assign to a variable up in the place we called it. (instead of making the assignment inside the function)

Comment thread code/script.js
const forecastWeather = filteredForecast[i].weather.description;
createFiveDaysInnerHTML(days, forecastTemp);
}
weatherContainer.innerHTML += createFiveDaysInnerHTML(days, forecastTemp, daysWeatherIcon);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function now returns the HTML (instead of assigning it to weatherContainer.InnerHTML) the assignment now happens here.

Comment thread code/script.js
const timeOfSunrise = getTimeOf(data.sys.sunrise, timezone);
const timeOfSunset = getTimeOf(data.sys.sunset, timezone)
changeHeaderInnerHTML(data, timeOfSunrise, timeOfSunset);
const weatherIcon = chooseWeatherIcon(data)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The icon now comes from the chooseWeatherIcon function (same one that we used for the five day forecast). So now the background img function is only for background img

Comment thread code/script.js
function getBackgroundWeatherClass(data) {
let main = data.weather.map((item) => item.main);
if (main.includes("Clouds")) {
weatherImg = cloudyIcon;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these are taken out because the other if/else function was doing the same thing

Comment thread code/script.js
if (main.includes("Clouds")) {
weatherImg = cloudyIcon;
backgroundImg.classList = "clouds";
return "clouds";

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these now return instead of assigning, the assigning is done when we call the function

Comment thread code/script.js


function createFiveDayForecast(filteredForecast) {
weatherContainer.innerHTML = ""

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearing the HTML for the five days as the first thing before rewriting it (it was in the event listener before)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant