Here is the normal solution,
function translatePigLatin(str) {
var csntArr = ["b", "c", "d", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "x", "z", "w", "y"];
var strArr = str.split("");
var newStr = "";
var i=0;
var leadCsntArr = Array();
if (csntArr.indexOf(strArr[i]) >= 0) {
while (csntArr.indexOf(strArr[i]) >= 0) {
leadCsntArr.push(strArr[i]);
++i;
}
newStr = str.substr(i) + leadCsntArr.join("") + "ay" ;
} else {
newStr = str + "way" ;
}
return newStr;
}
translatePigLatin("consonant");
Here is the RegExp solution, it is concise and simple. ^_^
function translatePigLatin(str) {
var newStr = "";
resArr = str.match(/[bcdfghijklmnpqrstvxzwy]*/);
if (resArr[0] === "") {
newStr = str + "way" ;
} else {
newStr = str.substr(resArr[0].length) + resArr[0] + "ay" ;
}
return newStr;
}
translatePigLatin("consonant");