PigLatin challenge - partly works, but getting error with certain words RESOLVED

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

Run your code through Python tutor. That should help you.

1 Like

Thanks @JohnnyBizzel! That helped a ton - I added break; to my for loop and it solved the problem.

1 Like

image

Looks like the first time str passes through your convertPigLatin function, it’s an array, so the join function works just fine. But the second time through, str is a string, which is why the join function fails.

1 Like

Thanks @JaceyBennett! I had to put a break; in the for loop to stop it. Sure appreciate you taking a look!

1 Like