Skip to content

C24 Crow Tatiana and Crow Hok Yin Iris Cheung, Weather Report#9

Open
FluenceMind wants to merge 15 commits into
Ada-C24:mainfrom
FluenceMind:main
Open

C24 Crow Tatiana and Crow Hok Yin Iris Cheung, Weather Report#9
FluenceMind wants to merge 15 commits into
Ada-C24:mainfrom
FluenceMind:main

Conversation

@FluenceMind

Copy link
Copy Markdown

No description provided.

@mikellewade mikellewade left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@FluenceMind and @Iris-Cheung-HY, nice work on your weather report. Thank you for the patience! Even instructors can have a back log!

Comment thread src/apicall.js
longitude = response.data[0].lon;
console.log('success in findLatitudeAndLongitude!', latitude, longitude);

return{latitude, longitude};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
return{latitude, longitude};
return { latitude, longitude };

Comment thread src/apicall.js
const axios = window.axios

export const findLatitudeAndLongitude = (cityName) => {
let latitude, longitude;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Declaring these variable here makes me think that they will be accessed somewhere in addition to the assignment located in your .then chain. Being they are only being declared here, I would suggest you move the declaration/assignment into the .then entirely. This would also be more secure since it would be locally scoped to the callback of the .then.

Suggested change
let latitude, longitude;

Comment thread src/apicall.js
export const findLatitudeAndLongitude = (cityName) => {
let latitude, longitude;

return axios.get('http://localhost:5000/location',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you would be using the base of this URL in multiple places, to make your code more D.R.Y by assigning a constant variable to it.

const BASE_URL = 'http://localhost:5000/'

Then it could be used in the following manner:

  return axios.get(`${BASE_URL}location',

Comment thread src/apicall.js
Comment on lines +13 to +14
latitude = response.data[0].lat;
longitude = response.data[0].lon;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
latitude = response.data[0].lat;
longitude = response.data[0].lon;
const latitude = response.data[0].lat;
const longitude = response.data[0].lon;

Comment thread src/apicall.js
.then((response) => {
latitude = response.data[0].lat;
longitude = response.data[0].lon;
console.log('success in findLatitudeAndLongitude!', latitude, longitude);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Don't forget to remove your console logs from your production code.

Comment thread src/index.js
Comment on lines +35 to +42
const resetInputButton = () => {
state.tempValue = '';
state.tempDisplay.textContent = `${state.tempValue}`;
state.skyDisplay.textContent = '';
state.landscapeDisplay.textContent = '';
state.cityNameInput.value = 'Seattle';
state.headerCityName.textContent = 'Seattle';
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread src/index.js
Comment on lines +44 to +61
const tempValueColorPatternChange = () => {
if (state.tempValue >= 80) {
state.tempDisplay.style.color = 'red';
state.landscapeDisplay.textContent = `🌵__🐍_🦂_🌵🌵__🐍_🏜_🦂`;
} else if (state.tempValue >= 70 && state.tempValue <= 79) {
state.tempDisplay.style.color = 'orange';
state.landscapeDisplay.textContent = `"🌸🌿🌼__🌷🌻🌿_☘️🌱_🌻🌷"`;
} else if (state.tempValue >= 60 && state.tempValue <= 69) {
state.tempDisplay.style.color = 'yellow';
state.landscapeDisplay.textContent = `"🌾🌾_🍃_🪨__🛤_🌾🌾🌾_🍃"` ;
} else if (state.tempValue >= 50 && state.tempValue <= 59) {
state.tempDisplay.style.color = 'green';
state.landscapeDisplay.textContent = `"🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲"`;
} else if (state.tempValue <= 49) {
state.tempDisplay.style.color = 'teal';
state.landscapeDisplay.textContent = `"🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲"`;
}
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Often when we find ourselves writing a plethora of conditional statements, we can usually make it more concise with a refactor. One possible refactor could be:
To make this a little more concise and avoid the multiple if statements we could use a loop and a list to do something like this instead:

const  temperatureRanges  = [
  {  min: 80,  color:  "red",  landscape:  "🌵 🐍 🦂 🌵🌵 🐍 🏜 🦂"  },
  {  min: 70,  color:  "orange",  landscape:  "🌸🌿🌼 🌷🌻🌿 ☘️🌱 🌻🌷"  },
  {  min: 60,  color:  "salmon",  landscape:  "🌾🌾 🍃 🪨 🛤 🌾🌾🌾 🍃"  },
  {  min: 50,  color:  "green",  landscape:  "🌲🌲⛄️🌲⛄️🍂🌲🍁🌲🌲⛄️🍂🌲"  },
  {  min:  -Infinity,  color:  "blue",  landscape:  "⛄️ ⛄️ ⛄️"  },  // Default for temperatures <= 49
];

for  (const  range of  temperatureRanges)  {
  if  (state.currentTemp  >=  range.min)  {
    tempValueElement.style.color  =  range.color;
    landscapeElement.textContent  =  range.landscape;
    break;  // Stop once the matching range is found
  }
}

What you gain in this implementation is DRY code and maintainability/scalability.

Comment thread src/index.js
Comment on lines +68 to +80
const updateSky = () => {
const skyChoice = state.skySelect.value;

if (skyChoice === 'Sunny') {
state.skyDisplay.textContent = '☁️ ☁️ ☁️ ☀️ ☁️ ☁️';
} else if (skyChoice === 'Cloudy') {
state.skyDisplay.textContent = '☁️☁️ ☁️ ☁️☁️ ☁️ 🌤 ☁️ ☁️☁️';
} else if (skyChoice === 'Rainy') {
state.skyDisplay.textContent = '🌧🌈⛈🌧🌧💧⛈🌧🌦🌧💧🌧🌧';
} else if (skyChoice === 'Snowy') {
state.skyDisplay.textContent = '🌨❄️🌨🌨❄️❄️🌨❄️🌨❄️❄️🌨🌨';
}
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note the consideration for the updating the temperature color.

Comment thread src/index.js
Comment on lines +82 to +92
const realTimeWeatherTemp = () => {
findLatitudeAndLongitude(state.cityNameInput.value)
.then(({latitude, longitude}) => getWeatherTemp(latitude,longitude))
.then(({weatherTemp})=> {
state.tempValue = weatherTemp;
state.tempDisplay.textContent = `${state.tempValue}`;
tempValueColorPatternChange();
}).catch((error) => {
console.log('error');
});
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread src/index.js
Comment on lines +94 to +123
const loadControls = () => {
state.addOneButton = document.getElementById('increaseTempControl');
state.minusOneButton = document.getElementById('decreaseTempControl');
state.tempDisplay = document.getElementById('tempValue');
state.landscapeDisplay = document.getElementById('landscape');
state.cityNameInput = document.getElementById('cityNameInput');
state.headerCityName = document.getElementById('headerCityName');
state.realTimeButton = document.getElementById('currentTempButton');
state.skySelect = document.getElementById('skySelect');
state.skyDisplay = document.getElementById('sky');
state.resetButton = document.getElementById('cityNameReset');
};
const registerEvents = () => {
state.addOneButton.addEventListener('click', addtempButton);
state.minusOneButton.addEventListener('click', minustempButton);
state.realTimeButton.addEventListener('click', realTimeWeatherTemp);
state.resetButton.addEventListener('click', resetInputButton);
state.cityNameInput.addEventListener('input', updateCityName);
state.skySelect.addEventListener('change', updateSky);
};

const onLoaded = () => {
loadControls();
registerEvents();
updateCityName();
updateSky();
};


onLoaded(); No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would suggest moving the logic of adding event handlers to their respective HTML elements into something like document.addEventListener("DOMContentLoaded", RegisterEventHandlers);. That way we register the event handler functions to run when the DOM content has fully loaded. This ensures all HTML elements and resources are available before attempting to attach event handlers or manipulate the DOM.

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