I have the following if, else if, else construct and I am just curious how I could convert such as construct into a switch statement.
var emailSubject = email.subject.toLowerCase();
if(emailSubject.indexOf("account request") >= 0){
//do acct req
}else if(emailSubject.indexOf("accounts pending removal for") >= 0){
//do account removal
}else if(emailSubject.indexOf("listserv application") >= 0){
//do listserv app
}else if(emailSubject.indexOf("student organization webmaster transfer request") >= 0){
//do webmaster xfer
}else{
//do default
}
My thoughts are but I do not think this is correct:
switch(emailSubject){
case this.indexOf("account request"):
//do acct request
break;
default:
//do default
}
Or
switch(0){
case emailSubject.indexOf("accounts pending removal"):
//process account pending removal
break;
default:
//do default behavior
}
Your example code cannot easily be converted to a switch statement in most languages, nor should it.
switchis for comparing a single variable against a range of constant values, whereas your logic requires comparison against non-constant values, with no variable to compare them with.if/else ifis the correct construction for your case.