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.
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.
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");
@camperextraordinaire 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.
// Intermediate Algorithm Scripting: Pig Latin
function translatePigLatin(str) {
// Find one or more consonants at the beginning
let cons_arr = str.match(/^[^aeiou]+/);
if(cons_arr){
let cons_str = cons_arr.join();
return str.substr(cons_str.length, str.length) + cons_str + 'ay';
}
else{
return str + 'way';
}
}
console.log(translatePigLatin("glove"));
My solution below + the explanation below that. It’s very regex heavy! I also opted for ternary operators instead of if statements since I never really use them!
RETURN If the first character is not a vowel then...
? test the whole string and see if it has no vowels
? if it has no vowels, add "ay" at the end of the word.
: if it has some vowels, search for the first group of consecutive consonants and move it to the end of the string. then add "ay" at the end of the string
: if the word begins with a vowel, add "way" at the end of the string.
function translatePigLatin(str) {
let regex = /^[^aeiou]+/
let matched = str.match(regex)
if (str.match(regex)){
return str.replace(regex, "").concat(matched + "ay");
} else{
return str.concat("way")
}
}
regex = matching starting characters that aren’t vowels.
matched = to catch the said characters.
if the str start with characters that aren’t vowels, replace those consonants with nothing ("") and concat those consonants at the end, adding “ay”.
else, just concat “way” at the end!