Pig Latin why my code return null

Tell us what’s happening:
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");

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:


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");

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

so stupid mistake :joy:

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