Skip to content

Instantly share code, notes, and snippets.

@noshaf
Created June 26, 2012 23:05
Show Gist options
  • Save noshaf/2999969 to your computer and use it in GitHub Desktop.
Save noshaf/2999969 to your computer and use it in GitHub Desktop.
Pig Latin Javascript
var translate = function(word) {
var array = word.split('');
var vowels = ['a','e','i','o','u'];
var newWord = '';
for(var i = 0; i < vowels.length-1; i++) {
for(var y = 0; y < word.length-1; y++) {
if(word[y] === vowels[i]) {
for(var x = y; x < word.length; x++){
newWord = newWord + word[x];
}
for(var n = 0; n < y; n++){
newWord = newWord + word[n];
}
return newWord + "ay";
}
}
}
}
translate("apple");
@seunzone
Copy link

seunzone commented Nov 3, 2017

`
function letters(word) {
return word.split('')
}

function pigLatinizeWord(word) {
var chars = letters(word);
return chars.slice(1).join('') + chars[0] + 'ay';
}

function pigLatinizeSentence(sentence) {
return sentence.replace(/\w+/g, pigLatinizeWord)
}

pigLatinizeSentence('try this')
`

@jessesanders
Copy link

jessesanders commented Mar 19, 2019

piglatinize(value) {
    let words = value.split(' '),
      newWords = [];

    for (var i = 0; i < words.length; i++) {
      newWords.push(this.translate(words[i]));
    }

    return newWords.join(' ');
  }

  translate(word) {
    let array = word.split(''),
      vowels = ['a', 'e', 'i', 'o', 'u'],
      newWord = '';

    for (var y = 0; y < word.length; y++) {
      if (vowels.includes(word[y])) {
        if (y === 0) {
          return word + 'way';
        }

        return word.slice(y, word.length) + word.slice(0, y) + 'ay';
      }
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment