travissanon
9/23/2017 - 4:00 AM

FCC - Pig Latin

'esversion: 6';
function translatePigLatin(str) {
  var vowels = ['a', 'e', 'i', 'o', 'u'];
  var string = str.split('');
  var vowelFirst = false;
  var vowelIndex;
  
  vowels.map(vowel => vowel === str.charAt(0) ? vowelFirst = true : false);
  
  for (var i = 0; i < string.length; i++) {
    vowels.map(vowel => {
      return string[i] === vowel ? vowelIndex = string.indexOf(string[i]) : false;
    });
    if (typeof(vowelIndex) === 'number') { break; }
  }
  
  if (vowelFirst === true) {
    string.push('way');
  } else if (typeof(vowelIndex) === 'number') {
    for (var j = 0; j < vowelIndex; j++){
      var index = string[j];
      string.push(index);
    }
    string.splice(0, vowelIndex);
    string.push('ay');
  } 
  
  return string.join('');
}

translatePigLatin("paragraphs");