Pig Latin, can we do it my way?

Hi guys I’ve spend several hours on it, and I can guess we can finish it with the indexOf function.
But I don’t know why I prefer my way and I really want to understand why my double for loop act this way.


function translatePigLatin(str) {
var voyelle = ["a","o","i","e","u"];
  var translated = "";
  str = str.split("");
  for (i=0; i<str.length; i++) {
    for(j=0; j<voyelle.length; j++) {
     
    if (voyelle[j] == str[0]) {
     translated = str.join("") + "way";
      
    }
if ( str[i] == voyelle[j]) {
 
  translated = str.slice(str.indexOf(str[i])) + str.slice(0,str.indexOf(str[i])).join("") + "ay";
    translated = translated.replace(/[^0-9a-z]/gi, '');        
     
  
}
      
      //translated = str.slice(str.indexOf(str[j])) + str.slice(0,str.indexOf(str[j])).join("") + "ay";
      //translated = translated.replace(/[^0-9a-z]/gi, '');        
        //str.slice(str.indexOf(str[j])) + str.slice(0,str.indexOf(str[j]));
     
    }
  }
  return translated;
}

translatePigLatin("glove");

I mean how this is working? How the two arrays are comparing eachother, can someone tell me step by step :slight_smile: ? Why he retain the “e” of glovel and not the first “o”?
It’s the last word which is’nt working…