Search code examples
javascriptif-statementfor-loopslicecharat

Trouble with making a piglatin converter


Okay so I'm having some trouble trying to get this to remove everything before the first vowel, adding it to the end, and then adding "ay" to the end of that

    function translate(word) { // put first constants at end && add "ay" 
      var result = word;
      var end = "WillBeOverrideN";
      for (var i = 0; i < result.length; i++) {
        if (result.charAt(i) === ("a" || "e" || "i" || "o" || "u")) {
          end = result.slice(0, i-1); // everything b/f vowel
          result = result.slice(i-1); // everything after && vowel 
          result += end; 
          break; 
        }
      }
      result += "ay";
      return result;
    }

Solution

  • you have to change function slightly, here is the working function

    function translate(word) { // put first constants at end && add "ay" 
          var result = word;
          var last = "WillBeOverrideN";
          for (var i = 0; i < result.length; i++) {
            if(["a","e","i","o","u"].indexOf(result[i]) !== -1){      
    
              last = result.slice(0, i); // everything b/f vowel
              result = result.slice(i); // everything after && vowel 
              result += last ; 
              break; 
            }
          }
          result += "ay";
          return result;
        }