pig latin

with regex

by trentHarlem

JavaScript

/* function translatePigLatin(str) {
  const vowels = 'aeiou'
  if (vowels.indexOf(str[0]) > -1) {
    return str + "way"
  } else {
    let firstVow = str.match(/[aeiou]/g) || 0;
    let vowel = str.indexOf(firstVow[0]);
    return str.substring(vowel) + str.substring(0, vowel) + "ay";
  }
} */

function translatePigLatin(str) {
  const vowels = 'aeiou'
  if (vowels.indexOf(str[0]) > -1) {
    return str + "way"
  } else {
    let firstVow = str.match(/[aeiou]/g) || 0;
    let vowel = str.indexOf(firstVow[0]);
    return str.substring(vowel) + str.substring(0, vowel) + "ay";
  }
}

console.log(
  translatePigLatin("eight"),
  translatePigLatin("consonant"),
  translatePigLatin('alabama'),
  translatePigLatin('glove'),
  translatePigLatin('trent'),
  translatePigLatin('martin'),
)
/* function pigLatin(str) {
  const vowels = 'aeiou'
  if (!vowels.includes(str[0])) {
    console.log(str.replace(/(\w+?)([aeiou]\w+)/i, '$2$1ay'))
    return str.replace(/(\w+?)([aeiou]\w+)/i, '$2$1ay')
  } else {
    console.log(`${str.slice(1)}way`)
    return `${str.slice(1)}way`
  }
} */

//const str = 'glove'
//const str = 'alabama'
//const firstVowel = /(\w+?)([aeiou]\w+)/i
//const firstVowel = /([aeiou])/i
//.  /([\sAEIOUaeiou]*[aeiouAEIOU])/
//console.log(str.split(firstVowel))