When I use switch statement , none of the cases matches the ‘prefix’.
But if I replace switch statement with if-else, everything works.
Why?
Thank you
//UPDATED
//el is DIV element. For example el.id='mph_4';
var prefix = /^[a-z]+/.exec(id);
//------------- SWTICH -------------------------
switch (prefix) {
case 'mph':
return 1;
case 'ph':
return 2;
case 'mh':
return 3;
}
//---------------IF-ELSE------------------------
if (prefix == 'mph') {
return 1;
}
else if (prefix == 'ph') {
return 2;
}
else if (prefix == 'mh') {
return 3;
}
RegExp.exec()returns an array. Soprefixis actually array and not a string. If you are sure that exec returns a single string, you can change your switch statement as: