Hello. I need some help with this test. I can’t pass it, it says “Should handle words without vowels”. I think the information is not enough to figure out this error.
Thanks for the help!
This is the Intermediate Algorithm Scripting: Pig Latin
Translate the provided string to pig latin.
Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”.
If a word begins with a vowel you just add “way” to the end.
Input strings are guaranteed to be English words in all lowercase.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
The test Should handle words without vowels. means that your code should work when you get words without vowels. I quickly did a test with your code and if I would take the non existent word: pkljdsk, in pig latin this would be pkljdskay but if I use your code, I get kpkljdsay. Your first letter is wrong.
Edit: This is because they say consonant or consonant cluster, so your full word is one cluster and moves to the back => stays the same. Currently one letter is moved.
The problem is when you run your test with a word like “rhythm”. Because none of the letters are vowels, the following else is triggered and i never gets beyond 0 (because of your i–).
This creates an infinite loop, because the for loop condition of i < str.length will be true forever.
This is not an endless loop. my outputs were correct running the tests (functions with parameters) in a jsbin online editor. it’s just that i couldn’t pass the “Should handle words without vowels.” test.
Run the above which is what the last test is testing and you will see an infinite loop. I put a console.log(‘i=’+i) just inside the for loop, so you can see it just keeps looping.
function translatePigLatin(str) {
let j = 0; // counter for words starting with vowel
const unmodifiedStr = str;
for (let i = 0; i < str.length; i++) {
if (str[i] === 'a' || str[i] === 'e' || str[i] === 'i' || str[i] === 'o' || str[i] === 'u') {
// starts with a vowel (without modifying string)
if (j === 0) {
return str + "way";
// if first index is a vowel (after modifying string)
} else {
return str + "ay";
}
// if letter is consonant at index 0.
} else {
str = str.slice(i+1,str.length) + str.slice(0,1);
i--; // return to index 0 until reaching end of str.length
j = 1; // to disallow evaluation of first conditional (j === 0) the second time around.
}
}
// exit loop, return if no vowels were found.
return unmodifiedStr;
}
translatePigLatin("rhythm");
Here. I removed the str.length. It’s still not passing the last test.
function translatePigLatin(str) {
let j = 0; // counter for words starting with vowel
const unmodifiedStr = str;
for (let i = 0; i <= 1; i++) {
if (str[i] === 'a' || str[i] === 'e' || str[i] === 'i' || str[i] === 'o' || str[i] === 'u') {
// starts with a vowel (without modifying string)
if (j === 0) {
return str + "way";
// if first index is a vowel (after modifying string)
} else {
return str + "ay";
}
// if letter is consonant at index 0.
} else {
str = str.slice(i+1,str.length) + str.slice(0,1);
i--; // return to index 0 until reaching end of str.length
j = 1; // to disallow evaluation of first conditional (j === 0) the second time around.
}
}
// exit loop, return if no vowels were found.
return unmodifiedStr;
}
translatePigLatin("rhythm");
@Pifko6 and @sebadev Instead of just posting full working solutions, it would be more helpful to the OP to give hints, suggestions, and examples of those hints and suggestions.
@RandellDawson you’re right, my bad. Im at the beginning of my coding adventure and got overexcited, I guess, haha. I will keep it in mind for the future, though.