Pig Latin why my code return null

Tell us what’s happening:
Hello,
I’m trying to find solution for this algorithm
My code is bellow
When first latter is consonant it working fine
but when first latter is vowel and I need only add “way” string at the end
the first latter of “way” ‘w’ becoming null
as example if I pass ‘algorithm’ code return ‘algorithmnullay’
What is wrong with my code?

Your code so far


function translatePigLatin(str) {
  const regEpresion = /^[b-df-hj-np-tv-z]+/
  
 return regEpresion.test ? str.replace(regEpresion, "") + str.match(regEpresion) + "ay" : str.concat("way"); 
  
  
}

const val = translatePigLatin("algorithm");
console.log(val)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.

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

here

function translatePigLatin(str) {
  
  let loop= str.split(/(?=[aeiou])/);
    return /^[aeiou]/.test(str)==true?str.concat("way"):str.slice(loop[0].length).concat(loop[0]+"ay");
 }
translatePigLatin("Consonant");

Hi smarpan,
thanks for suggestion :grinning:
I saw some solution of this algorithm
but my question was about why “way” the W latter becoming null in my code :thinking:

your condition for the ternary operation is always true.

regEpresion.test is true, because you actually don’t test it with str

1 Like

Thanks,
so stupid mistake :joy:


  1. b-df-hj-np-tv-z ↩︎