Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to a million IF statements

Using JavaScript I am pulling names out of webpage and stringing them together somehow (probably going with an array). Once I gather all the names together I need to make another string that gives all the email addresses of the names. The email addresses are not on the webpage so I will have to list every possible thisName=thisEmail in my script somehow. I was about to approach this with making a bazillion if statements but I thought there has to be a more efficient way. Any suggestions?

var x = getElementById("names");
var name = x.InnerHTML;
var email;
if (name == 'Steve'){ email == '[email protected]'; }
if (name == 'Bob'){ email == '[email protected]'; }
....
like image 520
Jonathan Eckman Avatar asked Apr 05 '12 12:04

Jonathan Eckman


People also ask

Why you should avoid if statements?

There is nothing wrong with using if-statements, but avoiding them can sometimes make the code a bit more readable to humans. This is definitely not a general rule as sometimes avoiding if-statements will make the code a lot less readable. You be the judge. Avoiding if-statements is not just about readability.

How do you write codes without an if statement?

i.e. you could just as easily write bool isSmall = i < 10; - it's this that avoids the if statement, not the separate function. Code of the form if (test) { x = true; } else { x = false; } or if (test) { return true; } else { return false; } is always silly; just use x = test or return test .


3 Answers

A switch statement, as your code is only if-elses :-)

No, honestly. The best thing would be if you'd find a simple algorithm to create an email address from any given name, like

function mail(name) {
    return name.toLowerCase() + "@gmail.com";
}
var email = mail("Bob") // example usage

If they differ to much, you might use an object as a key-value-map:

var mails = {
    "Steve": "[email protected]",
    "Bob": "[email protected]",
    ...
}
var email = mails[name];

You could also combine those, if you have to determine which algorithm you need to use:

var map = [{
    algorithm: function(name) { return name+"@something"; },
    names: ["Steve", "Bob", ...]
},{
    algorithm: function(name) { return "info@"+name+".org"; },
    names: ["Mark", ...]
}];
for (var i=0; i<map.length; i++)
    if (map[i].names.indexOf(name) > -1) {
        var email = map[i].algorithm(name);
        break;
    }

or when it is a bit simpler:

var domains = {
    "gmail.com": ["Steve", "Bob", ...],
    "free.xxx": ["Mark", ...],
    ...
};
for (var domain in domains)
    if (domains[domain].indexOf(name) > -1)
        var email = name.toLowerCase()+"@"+domain;
        break;
    }

Just try to reduce the amount of data to deliver to the client as much as you can.

like image 90
Bergi Avatar answered Nov 02 '22 22:11

Bergi


You can store all the email address in an associative array like

pseudo code

var emailsList = ["steve" => "[email protected]", "bob" => "[email protected]"];
then email = emailsList[name]; will solve your problem
like image 33
Sandeep Manne Avatar answered Nov 02 '22 22:11

Sandeep Manne


You could create an object in advance:

var name_email_map = {
 "Steve": "[email protected]",
 "Bob": "[email protected]",
 "John": "[email protected]"
}

This would be easy to output from some server side language with a JSON library for whatever language you're using. There is a list of JSON libraries at the bottom of this page: http://www.json.org/

If you're using PHP on the server side you can just json_encode an associative array, which you may have selected from a database.

var name = 'Bob'; //x.innerHTML;
var email = name_email_map[name];
alert(email); // Alerts [email protected]
alert(name_email_map['John']); // Alerts [email protected]
like image 23
Paul Avatar answered Nov 02 '22 23:11

Paul