I want to replace the 1st character of the string which isn’t a vowel const replacement = str.replace(str[i], '');
but how come it doesn’t stop? why does it continue to loop to the second character even if it is a vowel? thanks.
**Your code so far**
function translatePigLatin(str) {
const vowels = 'aeiou'
// loop string
for (let i = 0; i < str.length; i++) {
// loop vowels
for (let j = 0; j < vowels.length; j++) {
// condition if string starts with a vowel
if (str.startsWith(vowels[j])) {
return str + 'way';
}
// condition if string doesn't start with a vowel
else {
const replacement = str.replace(str[i], '');
console.log(replacement)
}
}
}
}
translatePigLatin("california");
/*
loop string
check if string begins with a vowel
if it does 'way' to the end
if it doesn't remove non vowel letters, add them to the end of the string and add 'ay' to the string.
return that string
*/
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36
Double check your logic here. Your loop over the vowels is inside of the loop over every single letter. But you have an if else inside of that loop. Every time the if condition doesn’t trigger, the else condition will instead. That replacement condition isn’t really what you want in that location. Also, it changes your original string in ways that will create surprising results. Your letters will shift, changing their indices.
Hmm, I was a bit off with the behavior of replace in my mind, but it’s still not where you want in to be. Also, shouldn’t that letter go somewhere instead of being deleted?
In words, your first iteration of the algorithm says
look at the first letter of my word
loop at the first vowel
if the first letter of the word is a then return the word with ‘way’ at the end
if not, remove the first letter
Now, you aren’t currently doing anything with the string where you removed the first letter, but this logic won’t work for ‘orange’. That starts with a vowel, but that vowel is after a, so your if statement would have removed the o in orange after finding that it isn’t an a.
Your if statement is inside of the loop. That means that every single time the if statement is not met inside of the loop, the else condition is triggered.
No. In words, your code says, ‘if the string does not start with vowel j, then remove the first letter of the string’. Every single time the if condition is not met, the else condition will execute. Every single time. This is inside of a loop over all vowels, so every single vowel that doesn’t match will cause the else statement to execute.
function translatePigLatin(str) {
const cluster = /^[^aeiou]+/gi;
const vowels = 'aeiou'
// loop string
for (let i = 0; i < str.length; i++) {
const vowelStart = false;
// loop vowels
for (let j = 0; j < vowels.length; j++) {
// condition if string starts with a vowel
if (str.startsWith(vowels[j])) {
vowelStart = true;
}
}
if (vowelStart) {
return str + 'way'
}
else if (str.charAt(0).indexOf(vowels === -1)) {
const subCluster = str.replace(cluster, '');
const returnedCluster = str.match(cluster).join();
return subCluster + returnedCluster + 'ay';
}
}
}
translatePigLatin("glove");
/*
loop string
check if string begins with a vowel
if it does 'way' to the end
if it doesn't remove non vowel letters, add them to the end of the string and add 'ay' to the string.
return that string
*/
I have made it work I am really happy!, could have I made it work with my previous logic only using slice? I couldn’t so that’s why I had used something else.