Weather app weird behaviour on converting from F to C

Hi everyone.

I finished the weather app but there is something bothering me.

This is my code: https://codepen.io/matiasdesousa/pen/KZQRKJ?editors=1111

If you see the changeUnit function i used °C and °F to check what unit to turn to when clicked.

However if change those to ℃ and ℉ respectively it doesn’t change the unit and it keeps multiplying by the temperature and displaying high wrong temperatures.

Can you tell why is this happening?

First thing I noticed is you have defined a unit argument for the changeUnit function but never pass it in when you call the function.

Yes, the way you are running the call too I’ve noticed you also only get the ‘else’ statement, or the main ‘if’ never fires even on the first run too, so rather than toggling back and forth it is performing:

temp = temp * 9 / 5 + 32;

over and over again.

The issue is that once an HTML entity has been rendered, it is no longer interpreted as an entity. ℃ becomes “ºC”, so your comparison on line 56 will always fail. There’s no need to use entities in this case, and it works if you simply use the character literals “ºC” and “ºF” instead, so I’d suggest going that route.

1 Like