1

I'm looking for the best solution here, i've got an idea but thinking it could be done prettier.

I'm making an simple weather application. And i'm using Yahoo Weather api were they have got codes for weather conditions.

Depending on the condition i'm giving a code. Now, there are 50 codes and i've categorised them into 5 categories. In my case ex. my categori Snow contains 15 of Yahoo's condition codes.

Well, if you got a better idea (which i bet there is) be free to suggest.

My thought is to return the matching value from a set of arrays, but not shure how to do it.

My code now looks like this:

function getCondition(code) {
   var snow = [1, 2, 3],
       sun = [4, 5, 6];
} 

What i need is the variable name that contains the matching number of the code?

I've made a JS-Fiddle http://jsfiddle.net/BH8r6/

3 Answers 3

1

The fastest lookup (translating a Yahoo code to your label) is to use the code as array key (if they are sequential).

var weather = [];
weather[0] = "no_weather"; 
weather[1] = "snow"; 
weather[2] = "snow"; 
weather[3] = "snow"; 
weather[4] = "sun";
weather[5] = "sun";
weather[6] = "sun"; 

function getCondition(code) {
   return weather[code];
}
Sign up to request clarification or add additional context in comments.

3 Comments

That would be one way to do it. Although i would like to 'bind' all my numbers to the category prior from doing it for every number. Thanks for your time!
I agree with you. Though, it depends on what you mean by "best solution". I'd say mine is the fastest, but the solution posted by @Engineer is the prettiest yet straightforward solution.
I agree.. I'm just dealing with many numbers, so in my case @Baz1nga solution is the best for my project. But i would prefeer both your and Engineer's metods in a case where there were less data.
0

Why dont you try an associative array when your key is your variable name and your values is the corresponding code for the variable name, thus your code will be something like this:

var myCodeArray=[];

myCodeArray["snow"]=[1, 2, 3];

myCodeArray["sun"] = [4, 5, 6];

now your method getCondition will be

function getCondition(code) 
{
    for(var definedCodeName in myCodeArray)
    {
      if(myCodeArray.hasOwnProperty(definedCodeName))
      {          
        var array=myCodeArray[definedCodeName ];
        for(var i=0;i<array.length;i++)
        {
            if(array[i]==code){
                return definedCodeName ;}
        }
      }
    }
    return "Not found";
}

Demo

2 Comments

my pleasure.. nt suggesting that this is the best approach but since you had an idea I am just giving you a way to solve with the same approach
Well, it was exactly what i was looking for. In my opinion it's a pretty way to do it since you have the numbers bound to an array. It's easy to read, easy to configure and maintain.
0

Why to complicate everything?! Just use 'switch' :

function getCondition(code) {
switch( code ){
    case 1:
    case 2:
    case 4:
    case 6:
        return "snow";
    case 3:
    case 8:
    case 9:
        return "sun";
    case 5:
    case 7:
    case 10:
        return "cloudy";        
}
return "none";
} 

2 Comments

That was my first thought as well. But i like the thought of binding the numbers to an array, and it saves me some mock-up when dealing with so many cases. - Thanks for your time!
@ChristianWerther, I am glad could help you somehow.

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.