Hi dear campers,
I’ve just finished my weather app, which you can see[ here].
(https://codepen.io/codablejoy/pen/PjONxv).
However, in a previous draft, I encountered a problem that I couldn’t figure out for the life of me. I was trying to change the background image using the “if … else” statements. However, the change worked on some weather conditions - such as “clear-day”, “fog”, but for the rest, including “rain”, “snow” etc. they all started showing the image that I set for the "“cloudy”||“partly-cloudy-day” condition. It is noteworthy that all those conditions that weren’t working was set behind the “cloudy”/"partly-cloudy-day conditions.
I therefore “switched” to the “switch” statement, and it all worked. However, I would really appreciate it if someone could explain why the “if … else” function didn’t work at the first place.
The code is below. And you can see the draft project here. Thanks in advance!
if(newWeather === “fog”)
{
$(‘body’).css(“background-image”, ‘url(“http://unsplash.com/photos/uvbA1IHilCI/download?force=true”)’);}
else if(newWeather === “clear-day”){
$(‘body’).css(“background-image”, ‘url(“http://unsplash.com/photos/hVF_04fzKO4/download?force=true”)’);}
else if(newWeather === "cloudy"||"partly-cloudy-day")
{
$('body').css("background-image", 'url("http://unsplash.com/photos/KFnu4Y0uNJA/download?force=true")');}
else if(newWeather === "clear-night"||"partly-cloudly-night")
{
$('body').css("background-image", 'url("http://unsplash.com/photos/wGC7lGah18E/download?force=true")');}
else if(newWeather === "rain")
{
$('body').css("background-image", 'url("http://unsplash.com/photos/Kwi60PbAM9I/download?force=true")');}
else if(newWeather === "snow"||"sleet")
{
$('body').css("background-image", 'url("http://unsplash.com/photos/n1h2kVc549c/download?force=true")');}
else if(newWeather === "wind")
{
$('body').css("background-image", 'url("http://unsplash.com/photos/kmF_Aq8gkp0/download?force=true")');};