Need Help with pig-Latin project

I’m trying to do the pig Latin exercise, right now I’m stuck trying to get the new set of stings with the translated pig Latin to appear in my second text box. I’ve started with the vowels first because I thought that would be easier to do, but every time I try to translate to pig Latin my second text box return empty even after I returned my new str variable to equal what ever value is in my English text area, and also am I using the while loop correctly this is my first time using a different loop other than for,

here is a link to my code

I’m not quite sure why you’re using a while loop as you’re only checking the first index.
Your code doesn’t actually enter the loop though as


returns undefined. You can check this by using console.log()

When using methods you will typically use round brackets like this: indexOf()

One way to test for the vowel is like this:

// Use includes() to test if the first letter of english is in vowel array
      if(vowels.includes(english.value.charAt(0))) {
        // it is so perform the concat
        newstr = english.value.concat("way");

You should now see the result popping up in the second text box.

Note that this is a tricky problem and so it would be worth spending some time going through some of the available string and array methods and make sure you’re familiar with how to use them.

Best of luck!! :smiley:

I need help with one more thing. I’ve got the function to translate to pig-Latin if the first character is a constant. by slicing char 0 and 1 for one variable and slicing out char 0 for variable like this.
let slice1 = string.slice(0,1);
let slice2 = string.slice(1);
and adding them together with ay. This makes sense if the second char is a vowel, but the one problem I’m having is if the word has multiple starting constants like the word “cluster” the word has two letters for constants, and the third letter is a vowel. I tried to fix this by making a variable that would return the first occurrence of a one of the vowels that’s in my array, but this doesn’t seem to work, and really stuck on this.

You’re trying to find the first vowel which is good and the slices are fine with that, however your approach to finding the first vowel doesn’t work.

var firstVowel = string.indexOf(vowels);

Vowels is an array of chars and so will never exist as an index of a string.

Use console.log(firstVowel) to check what firstVowel is. It’s really important to learn how to debug and this is the first step. You’ll discover that firstVowel is always -1 which means that the index wasn’t found. It’s worth reading the docs on this method so you understand this:

You also have another smaller issue with the code which is the else if statement:

else if (vowels !== string.charAt(0))

You’re comparing an array (vowels) to a char (string.charAt(0)). These are never the same and so using !== will always return true. In this case, using an else statement would give you the same result and will be fine.
Remember, use console.log() to check the value of things and see what your code is doing.

So how to go about getting the first vowel? You’ll need to loop through the chars of the string and check if that is in the vowel array. Once you find the vowel, set firstVowel to the index and break out of the loop.
In pseudocode that would look something like:

loop through string:
  if letter is included in vowels:
    firstVowel = index of letter in string

Have a go at implementing this!
Here’s a link to a useful loop type:…of

If you're still struggling then expand this for a walkthrough but please have a go first
// firstVowel is declared (currently undefined)
    let firstVowel;

    // use the for...of loop to iterate through string
    for (const letter of string) {
        // check if the current letter is included in vowel array
        if (vowels.includes(letter)) {
            // set firstVowel to the index of string
            firstVowel = string.indexOf(letter);
            // break out of the loop as we've found the firstVowel

If the word you are testing doesn’t have a vowel then firstVowel will be undefined. You can use this to handle that case.

Hopefully this helps out.
From here you can add support for: words with no vowels, multiple words and the qu case (which is slightly more difficult but very manageable).

thank you for all your help that was a tough exercise.