Weather app weird behaviour on converting from F to C

Weather app weird behaviour on converting from F to C
0.0 0

#1

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?


#2

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.


#3

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.


#4

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.