Tell us what’s happening:
For some reason, my code for the pig latin challenge works except with california. What I don’t understand is if I send california to the function, it calls a TypeError: str.join is not a function (it works with all the other words in the challenge). But if I change the str.join(’’) to str.join(’ ') after submitting california, I don’t get the error. (However it returns aliforniacay with spaces).
Any suggestions would be helpful!
Thank you!
Your code so far
<spoiler>
function translatePigLatin(str) {
var key = ['a','e','i','o','u'];
str = str.split('');
//check to see if first letters of string is a vowel
for (var i = 0; i < str.length; i++){
if(str[0] === key[i]){
//if vowels are at the begining, just add way to the end
str = str.join('');
str += 'way';
return str;
}
}
//if there are up to 3 consanants at the beginning of the string
for (var j = 0; j < key.length; j++){
if(str[1] === key[j]){
convertPiglatin(1);
} else if (str[2] === key[j]){
convertPiglatin(2);
} else if (str[3] === key[j]){
convertPiglatin(3);
}
}
//move all consonants from the beginning to end of string
function convertPiglatin(num){
str = str.join('');
var newStr = str.slice(0,num);
var strLength = str.length;
str = str.substr(num, strLength);
str = str + newStr + 'ay';
}
return str;
}
translatePigLatin("consonant");
</spoiler>
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/pig-latin