By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

Lookup table...if / else vs. switch vs. hash?

P: n/a
cjl
Hey all:

I need to get some user input which will be a six digit number. I will
split it into two character pairs. Each pair will need to be "looked
up", for example:

if firstPair == '01' then variable = "Monkey";
else if firstPair == '02' then variable = "Chicken";
etc..
So I think I could do this with if / else statements chained together,
and I think I could do this with a switch statement.

I remember someone showing a way to do this with a hash, but I can't
find it anywhere.

Is there a javascript idiom that is more elegant for this problem than
if / else or switch?

-CJL

Oct 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 13/10/2005 13:26, cjl wrote:

[snip]
if firstPair == '01' then variable = "Monkey";
else if firstPair == '02' then variable = "Chicken";
etc..
A lookup table would be the nicest solution:

var pairs = { '01' : 'Monkey',
'02' : 'Chicken' },
variable = pairs[firstPair];

[snip]
I remember someone showing a way to do this with a hash, but I can't
find it anywhere.


Note that the object above, pairs, is not a hash table. It's just an
object that's being used like a lookup table, where property names are
mapped to values.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 13 '05 #2

P: n/a
cjl
Mike:

Thanks. That is exactly what I was looking for.

I guess I may have been using the terms lookup table and hash
incorrectly, but that's what I get for trying to sound like I know
about computer programming using terms that I've seen on Slashdot.

-CJL

Oct 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.