Pig Latin got me lost on .length

Pig Latin got me lost on .length
0

#1

Hey Guys,
I was just doing Pig Latin and I managed to figure it out alright, but I came across something weird.
When I used the second regex to select the initial consonants, it logs them, but the .length property only comes up as 1?
I just tested it again and even 5 letters shows up as .length = 1

Is there something I’m missing here?

Your code so far


function translatePigLatin(str) {
	let regex = /\b[aeiou]/g;
	let start = str.match(/\b[^aeiou]+/g);
	return regex.test(str) ? (str) => str.concat("way") : remove(str);

	function remove(str) {
		console.log(start);	           // logs ['gl']
		console.log(start.length);  //  logs 1  ???
		console.log(str.length);	 //   logs 5
		return str.replace(/\b[^aeiou]+/g, "").concat(start + "ay");
	}
}
translatePigLatin("glove");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin


#2

start is an array with one element (a string), so the length of start is 1. The length of the first element of start is 2.


#3

Ahh, I see. It’s clearly written in the docs too :stuck_out_tongue_closed_eyes:.

Could I also ask, what would be the best way to condense the second function “remove()” to arrow function syntax?

When I get it to

(str) => str.replace(/\b[^aeiou]+/g, "").concat(start + "ay")

and place it as the second expression, I just receive [Function] as my answer?

Are some longer functions best left in long format?


#4
const remove = (str) => str.replace(/\b[^aeiou]+/g, "").concat(start + "ay");

NOTE: In order to use a const variable, it must be declared before it can be referenced.


#5

I noticed you are referencing the same regular expression (see below) in two places.

/\b[^aeiou]+/g

Why not create a variable for this regular expression? Also, I recommend giving each regular expression variable a name which better explains each one’s purpose. It will help make your code more readable.


#6

Ok, I’ve refactored the function to make it more readable. I tried declaring the separate functions before the ternary operator and just having the variables as the expressions but I just receive [Function] as the answer in my repl, and it doesn’t pass the tests in FCC.

I’m sure I did as you mentioned in the previous comment but somethings going wrong?

Without Arrow syntax

function translatePigLatin(str) {
	const reFindVowel = /\b[aeiouAEIOU]/g;
	const reFindConsonant = /\b[^aeiouAEIOU]+/g;
	return reFindVowel.test(str) ? append(str) : remove(str);

	function append(str) {
		return str.concat("way");
	}

	function remove(str) {
		let start = str.match(reFindConsonant);
		return str.replace(reFindConsonant, "").concat(start + "ay");
	}
}

translatePigLatin("eight");

With Arrow syntax

function translatePigLatin(str) {
	const reFindVowel = /\b[aeiouAEIOU]/g;
	const reFindConsonant = /\b[^aeiouAEIOU]+/g;
	const append = str => str.concat("way");
	let start = str.match(reFindConsonant);
	const remove = str => str.replace(reFindConsonant, "").concat(start + "ay");
	return reFindVowel.test(str) ? append : remove(str);

}

translatePigLatin("eight");

#7

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

I am busy right now, but hopefully someone else can take a look for you.