PigLatin Translator

javascript
#1

Hey everyone!

So I am almost done with my piglatin translator, however I am stuck on one last problem… The only test I am failing is this one.

it('should translate a complex word', () => {
      assert.equal(pigLatin('create'), 'eatecray');
      assert.equal(pigLatin('valley'), 'alleyvay');
    });

So basically, I am stuck due to not fully understanding the type of words I am testing for. I see in the test it’s create and valley, but what other types of words share this complexity. I am unsure what code to write because I don’t know exactly the types of words I’m trying to filter for. Valley looks and translates like a normal word, however create takes the first two letters away to move toward end.

I hope this makes sense to you all. Thank you for your time!

#2

okay, so I found this. I believe this is the type of complex words were solving for!!

  • Words beginning with consonant clusters would change as follows: the word “switch” would become itch-sway , the word “glove” would become ove-glay and the term “fruit smoothie” would become uit-fray oothie-smay .
#5

What I did is find the index value of the first vowel in a word, using indexOf and match(), and used that index # to split the word at its location using substring().

// define a regular expression with search parameters:
let regex = /[aeiou]/;
// define the index value of the first vowel in the word:
let indexedVowel = word.indexOf(word.match(regex)[0]);
// make substrings using the vowel location, append in the right order to make pig latin:
word = word.substring(indexedVowel) + word.substring(0, indexedVowel) + 'ay';

Helpful topics on MDN for me were Regular Expression, indexOf(), match(), and substring()!

1 Like
#6

sweet, and that’ll probably work for the other issue i have as well. I’m trying to get the word to check for any value in an array. arrays such as vowel or prefix. So that is super helpful. I’ll just have to change the way I’m trying to code it.

#7

For anyone else reading this, Heather & I have been spitballing about how to solve this homework with arrays/for loops; it can be done by looping through the letters of the input word, and then looping through vowels stored in an array! The code looks like this:

function pigLatin(a) {
  let word = a.toLowerCase().trim();
  const vowels = ['a','e','i','o','u'];
  for( let i = 0; i <= word.length; i++ ){
    for( let v = 0; v <= vowels.length; v++ ){
      if( word[0] != vowels[v] ){
        if( word[i] == vowels[v] ){
          // code here runs if a vowel is not the first letter;
          // variable 'i' from the for loop is thus the location of first vowel
        }
      } 
      else {
        // code here runs if a vowel is the first letter
      }
    }
  }
}
1 Like
#8

Yes! This works too! For anyone using arrays and having a hard time!

let vowel = ["a", "e", "i", "o", "u"];
for (let i = 0; i <= word.length; i++){
    for(let v = 0; v <= vowel.length; v++){
      if (word[0] == vowel[v]){
        // code goes here
      }
    }
  }