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

C22- Elzara and Priyanka- weather-report #2

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

Conversation

PriyankaKaramchandani
Copy link

No description provided.

Copy link

@yangashley yangashley left a 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!

Comment on lines +1 to +11
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');

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

Comment on lines +18 to +32
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');
}
};

Choose a reason for hiding this comment

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

Be mindful of indentation

Suggested change
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');
}
};

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", "🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲"],
]

Comment on lines +38 to +46
if (temperature >= 80) {
landscapeDisplay = '🌵__🐍_🦂_🌵🌵__🐍_🏜_🦂';
} else if (temperature >= 70) {
landscapeDisplay = '🌸🌿🌼__🌷🌻🌿_☘️🌱_🌻🌷';
} else if (temperature >= 60) {
landscapeDisplay = '🌾🌾_🍃_🪨__🛤_🌾🌾🌾_🍃';
} else {
landscapeDisplay = '🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲';
}

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.

Comment on lines +55 to +67
const addOneTemp = () => {
temperature += 1;
tempDisplay.innerText = temperature;

updateDisplay()
};

const reduceOneTemp = () => {
temperature -= 1;
tempDisplay.innerText = temperature;

updateDisplay()
};

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

Comment on lines +69 to +77
increaseTemp.addEventListener('click', addOneTemp);
decreaseTemp.addEventListener('click', reduceOneTemp);

const updateCityName = () => {
const cityText = cityNameInput.value;
cityNameDisplay.innerText = cityText;
};

cityNameInput.addEventListener('input', updateCityName);

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

Comment on lines +130 to +135
skyOptions.forEach(option => {
const optionElement = document.createElement('option');
optionElement.value = option.name.toLowerCase();
optionElement.textContent = option.name;
skySelect.appendChild(optionElement);
});

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 = [

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.

Suggested change
const skyOptions = [
const SKY_OPTIONS = [

Comment on lines +110 to +111
const temperatureKelvin = weatherData.main.temp;
temperature = Math.round((temperatureKelvin - 273.15) * 9/5 + 32);

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

Comment on lines +82 to +83
return axios
.get(`http://localhost:5000/weather?lat=${lat}&lon=${lon}`)

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

Suggested change
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}`)

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

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