Hello. Could I request some assistance with the pigLatin algorithm challenge? Am I on the right track, and if so, how do I get rid of the infinite while-loop I’ve unintentionally set up. Thanks!
(Code here):
function translatePigLatin(str) {
var regex = /[aeiou]/gi;
var i = 0;
if (str[0].match(regex)) {
str.push("way");
} else {
while (!str[i].match(regex)) {
var sliced = str.slice(i, i+1);
str.push(sliced);
i++;
}
str += 'ay';
return str;
}
translatePigLatin("consonant");
Thanks! Although I’m able to move the first character in the str to the end (see below), I am having difficulty writing a for or while loop that iterates over consonant clusters. I wondered if I’m on the right track, or if I should try a different approach.
var str = "school";
var i = 0;
var regex = /[aeiou]/gi;
var sliced = str.slice(i, i+1);
str = str.slice(1) + sliced;
i++;
var str = "school";
var i = 0;
var regex = /[aeiou]/gi;
var consonantCluster = '';
while (!regex.test(str[i])) {
consonantCluster += str[i++];
}
console.log(consonantCluster); // 'sch'
OR
var str = "school";
var regex = /[aeiou]/gi;
for (var i = 0, consonantCluster = ''; i < str.length; i++) {
if (regex.test(str[i])) break;
consonantCluster += str[i];
}
console.log(consonantCluster); // 'sch'