Skip to content
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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

alyssa-tn
Copy link

No description provided.

Copy link

@spitsfire spitsfire left a 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'

Choose a reason for hiding this comment

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

Suggested change
'use strict'
'use strict';

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);

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.

Comment on lines +7 to +31
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 = '🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲';
};
}

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.

Comment on lines +131 to +140
const getCurrentTemp = () => {
locationCall()
.then((temp) => {
tempElement.textContent = parseInt(temp);
tempValue = parseInt(temp);
})
.catch((error) => {
console.log('The value of error is:', error);
});
}

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!

Comment on lines +145 to +156












Choose a reason for hiding this comment

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

Suggested change

} else {
tempElement.style.color = 'blue';
};
}

Choose a reason for hiding this comment

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

Suggested change
}
};

} else {
landscapeElement.textContent = '🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲';
};
}

Choose a reason for hiding this comment

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

Suggested change
}
};

tempElement.textContent = tempValue;
colorTemp();
addLandscape();
}

Choose a reason for hiding this comment

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

Suggested change
}
};

All assignments should have a semicolon at the end


const updateCityName = () => {
cityNameElement.textContent = cityNameInput.value;
locationCall()

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?

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.

3 participants