Right now I’m working through the “Show the Local Weather” Intermediate Front End Project, and I’m using a series of if statements based on text from the FCC Weather API. Here’s a code snippet:
if (json.weather[0].main == "Clear"){
$("#current-weather").html("<i class=\"wi wi-cloud\"> </i>");
}
It works fine, and I’m currently using a chain of if else statements like the one above to go through the data from the API and show a weather icon based on the “main” property (weather icons are from weathericons.io).
But I’m thinking about using a switch statement using the API’s weather “id” value to determine the case. This means that I can be a lot more specific about which icon is used, and of course it’ll make the code a lot more readable.
Switch statements are things that I’ve only ever come across in tutorials but I’ve never really seen them in actual code written by real people.
I’d like to know if this is an appropriate time to use a switch statement, or if I’m just over engineering my coding solution. Please let me know what you think. I’m trying to pick up good coding practice.