Pig Latin Coding Help

**Tell us what’s happening:
I have been working on this code for a while and have been trying to make it work but it doesn’t seem to be working. I can do it while with vowels, it’s really the non-vowels that aren’t working. The vowels do work. I think it has something to do with either the while statement or the StrConcat variable, but I am not sure.

**
Describe your issue in detail here.

  **Your code so far**

function translatePigLatin(str) {
let reVowels = /[aeiou]/gi;
let returnVal = '';
if (str[0].match(reVowels)){
  returnVal = str + "way"; 
  return returnVal; 
} else if(str[0].match(reVowels) == false){
 let cRegex = /^[^aeiou]+/;
 let strConcat = str.match(cRegex); 
 str.replace(cRegex,''); 
 str.concat(strConcat); 
 returnVal = str +  "ay"; 
 return returnVal; 
}

}

console.log(translatePigLatin("paragraphs")); 
  **Your browser information:**

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

Challenge: Pig Latin

Link to the challenge:

.replace() and .concat() do not change the original string. They return a new string.

Try logging str after each of these lines.

1 Like

I tried to log the strings after, it still showed undefined, I changed to code to register the work to a new string, but it is still showing undefined.

function translatePigLatin(str) {
  let reVowels = /[aeiou]/gi;
  let returnVal = '';
  if (str[0].match(reVowels)){
    returnVal = str + "way"; 
    return returnVal; 
  } else if(str[0].match(reVowels) == false){
   let cRegex = /^[^aeiou]+/;
   let strConcat = str.match(cRegex); 
   let newStr = str.replace(cRegex,'');
   let finalStr = newStr.concat(strConcat);
   returnVal = finalStr + "ay"; 
   return returnVal; 
  }
  
}

console.log(translatePigLatin("paragraphs")); 

What does .match() return? Does it ever return false?

You have two ifs (an if and an else if). Can they both be false? if so, what is the expected behavior?

Sorry, it took me a while, thank you for the help, I was trying to get it to work, figured out to use test instead of match, and I think I’ve understood it, yet it is still returning undefined when it comes to the final concat.

function translatePigLatin(str) {
  let reVowels = /[aeiou]/gi;
  let returnVal = '';
  if (reVowels.test(str[0]) == true){
    returnVal = str + "way"; 
    return returnVal; 
  } else if(reVowels.test(str[0]) == false){
   let cRegex = /^[^aeiou]+/;
   let strConcat = str.match(cRegex); 
   let newStr = str.replace(cRegex,'');
   console.log(newStr); 
   let finalStr = newStr.concat(strConcat);
   console.log(finalStr);
   returnVal = str + "ay"; 
  }
}```

Do you ever actually return this returnVal?

Oops! Sorry, thank you for all your help, really appreciate it!

I’m glad I could help. Good job working through a challenging problem. Happy coding!