Garmaine Staff asked 1 year ago

I use the switch statement in my code often, I read about the object and a lot of developers say that is better to use. I want to use less code, do you guys know an alternative way of making this code more efficient?

full code link here – https://jsfiddle.net/lmanhaes/5ebjypo1/

I shared two examples below that I would like to change.

Many thanks.

//fisrt example
$(document).ready(function () {

    $("#country").change(function () {
        var cityName;
        var select = $("#country option:selected").val(); 
        switch (select) {
            case "England":
                cityName = "england";
                city(cityName);
                break;
            case "Scotland":
                cityName = "scotland";
                city(cityName);
                break;
            case "Wales":
                cityName = "wales";
                city(cityName);
                break;
            case "Northern Ireland":
                cityName = "nireland";
                city(cityName);
                break;
            default:
                $("#city").empty();

                break;
        }
    });

    $("#city").change(function () { 
        var cityValue = $("#city").val();
        getWeatherApi(cityValue);
    });
    
 //second example
    
    $.each(response.weather, function (index, value) {  
                    cond = response.weather[index].main;

                    

                    switch (cond) { 
                        case "Clouds":
                            icon = '<img src="./weather_icons/cloud.png" alt="cloud" width="70px" height="80%"/>';
                            break;
                        case "Hail":
                            icon = '<img src="./weather_icons/hail.png" alt="hail" width="70px" height="80%"/>';
                            break;
                        case "Heavy Cloud":
                            icon = '<img src="./weather_icons/heavy cloud.png" alt="heavy-clouds" width="70px" height="80%"/>';
                            break;
                        case "Heavy Rain":
                            icon = '<img src="./weather_icons/heavy rain.png" alt="heavy-rain" width="70px" height="80%"/>';
                            break;
                        case "Rain":
                            icon = '<img src="./weather_icons/rain.png" alt="rain" width="70px" height="80%"/>';
                            break;
                        case "Sleet":
                            icon = '<img src="./weather_icons/sleet.png" alt="sleet" 70px" height="80%"/>';
                            break;
                        case "Snow":
                            icon = '<img src="./weather_icons/snow.png" alt="snow" width="70px" height="80%"/>';
                            break;
                        case "Sun":
                            icon = '<img src="./weather_icons/sun.png" alt="sun" 70px" height="80%"/>';
                            break;
                        case "Sun and Clouds":
                            icon = '<img src="./weather_icons/sun and cloud.png" alt="sun-cloud" width="70px" height="80%"/>';
                            break
                        case "Thunderstorm":
                            icon = '<img src="./weather_icons/thunderstorm.png" alt="thunderstorm" width="70px" height="80%"/>';
                            break;
                        default: 
                            icon = '';
                            break;
                    }
                });