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
Ahh, I see. It’s clearly written in the docs too .
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?
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.
1 Like
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");