0

I'm using Yahoo Weather and there are about 47 weather conditions from 0 to 47 each number represents the condition of the weather.

I want to get the condition for 4 days , today and the next 3 days so there will be a very long code of switch statements if I use a switch statement for each.

My code now for today condition:

var src = ""; //This variable will contain an icon that represents the weather condition.

switch(todayCondition){ //todayCondition is today condition it's in the range [0-47]

    case "0":
        src = 'storm.svg';
    break;
    ........
    ........
    case  "47":
        src = 'rain.svg';
    break;

}

document.getElementById('todayWeatherIcon').src = src;

The html:

 <img id = 'todayWeatherIcon' />

There are 3 other variables for the next 3 days conditions that will be from 0-47 too and will have the same icons depending on the number.

How to do the same thing for the other 3 variables without repeating the same code?

4
  • 2
    Are all your icons named "condition<some number>Img.png"? If so, why not get rid of the switch completely and just do src = "condition" + todayCondition + "Img.png";? Then repeat that for each day. Commented Feb 16, 2018 at 23:29
  • 1
    Sorry , It seems that you miss understand me and I didn't explain it well , The icons names are not "condition" + todayCondition + "Img.png"; , It's just an indication , The icons are like cloudy.svg , rain.svg , ... etc Commented Feb 16, 2018 at 23:37
  • 1
    I will edit the question Commented Feb 16, 2018 at 23:38
  • @peter, but if your "condition" always generates ints from 0..47, you can use var src = arrayOfIconNames[todayCondition]; Commented Feb 16, 2018 at 23:51

4 Answers 4

2

there is no need for multiple switch statement, since you have a fixed file name with each weather condition number in the filename you could just do this

var src = "";
// concatenate todayCondition with the rest of the file name
src = "condition" + todayCondition + "Img.png";

document.getElementById('todayWeatherIcon').src = src;

Note: You should only do this if you know the names of the file won't change in the nearest future

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry , It seems that you miss understand me and I didn't explain it well , The icons names are not "condition" + todayCondition + "Img.png"; , It's just an indication , The icons are like cloudy.svg , rain.svg , ... etc
it's either you use the switch statement, or you use arrays and set their index to the possible weather values just as @Brettc specified.
1

You could just set the condition like so

src = 'condition'+todayCondition+'Img.png'; document.getElementById('todayWeatherIcon').src = src;

2 Comments

Sorry , It seems that you miss understand me and I didn't explain it well , The icons names are not "condition" + todayCondition + "Img.png"; , It's just an indication , The icons are like cloudy.svg , rain.svg , ... etc
Could you put your conditions in an array and then just call the array location? arry[4] = "cloudy.png", arry[5]="sunny.png"...
0

You should simply use a function :

function getIcon(weatherCondition) 
{
    var src = ""; //This variable will contain an icon that represents the weather condition.
    switch(weatherCondition){ //weatherCondition is the weather condition it's in the range [0-47]
        case "0":
            src = 'storm.svg';
            break;
        ........
        ........
        case  "47":
            src = 'rain.svg';
            break;
    }
    return src;
}

var day1Condition = getIcon(todayCondition);
var day2Condition = getIcon(tomorrowCondition);
...
document.getElementById('todayWeatherIcon').src = day1Condition;
document.getElementById('tomorrowWeatherIcon').src = day2Condition;
...

Comments

0

If the image names are all different then you can best use an array of strings, like this:

var images = ["cloudy.svg", "sunny.svg", "rainy.svg"];

// Arrays are designed to work with numeric index values:
console.log(images[0]);
console.log(images[1]);
console.log(images[2]);
console.log("--------------")

// Javascript also accepts "numeric strings" as array index values:
console.log(images["0"]);
console.log(images["2"]);
console.log("--------------")

// Or using a variable, this is the closest to what you need to do:
var todayCondition = "1";
var src = images[todayCondition];
console.log(src);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.