Almost there, need help - Pig Latin exercise

I’m working my way through this and I’m almost there. I feel like I’m missing something small, particularly with the if statement at the bottom of my code. All checks out except for one result. I thought it would work when I introduced additional ‘If else’ statements to output the other results, but it fails. I left additional ‘if else’ statements out and am just using one if statement and one return. ‘if else’ statements are pretty straight forward so I’m not sure what I’m missing.

Here is what I have so far:

/* jshint esversion: 6 */

function translatePigLatin(str) {
var add1 = “ay”;
var add2 = “way”;
var vowels = [“a”, “e”, “i”, “o”, “u”];

 //split 1
 var firstchop = str.split('');
 var firstl = firstchop.shift();
 firstchop.push(firstl);
 //1st and 2nd Result
 var firsttwo = firstchop.join('') + add1; 

 //split2
 var secondchop = str.split('');
 //filter how many consonants
 var howmanycons = secondchop.filter((val) => {
      for (var l=0;l<vowels.length;l++){
      if(val == vowels[l] || secondchop.indexOf(val) > 2){
        return false;
      }
    }
    return true;
 });
 //transform filter result to length
 var cutby = howmanycons.length;
 var chop2 = secondchop.slice(cutby);
 //Gets 3rd result using length to cut and join   
 var middle = chop2.join('') + howmanycons.join('') + add1;
    
 //Last two results
 var lasttwo = str + add2;

//final results
for (var i=0;i<vowels.length;i++){
if (str[0] == vowels[i]) {
return lasttwo;
}
//if I add another if else here it doesn’t work…
}
return firsttwo;
}

translatePigLatin(“california”);

replace all the “ ” with " " or ’ '
to be honest i didnt check logic , but you’ll have to do the replacement anyway))

I think the first part is really confusing, and might be where the problem lies. I suspect you’re trying to do too much at once which makes it harder to reason about.

Consider instead breaking this function up into smaller functions and use those to solve the problem, perhaps using the following hint:

How could you use a function that tells you where the first vowel is to solve the problem?

Thank you for taking a look. The quotation formatting was changed when it was carried over. The originals are using " ".

Thanks gebulmer. I’ll run with your suggestion and continue working on it. Best.

I found a way to solve it. Let me know what you think:

/* jshint esversion: 6 */
function translatePigLatin(str) {
var vowels = [“a”, “e”, “i”, “o”, “u”];
var word = str.split(’’);
//filter for vowels
var find = word.filter((val) => {
for (var e=0;e<vowels.length;e++){
if(val == vowels[e]){
return true;
}
}
return false;
});
var splitbyvowel = find.shift();
function rearrange() {
if (word[0] != splitbyvowel) {
return str.substr(str.indexOf(splitbyvowel), str.length) +
str.substr(str.indexOf(str[0]), str.indexOf(splitbyvowel)) + “ay”;
}
return str + “way”;
}
return rearrange();
}

translatePigLatin(“eight”);

Looks pretty good! (sorry for the slow reply)

I think it can be simplified by a lot though, you can use a regex function to find the index of the first vowel in the following way:

var vowels = new RegExp("[aeiou]");
var matchIndex = vowels.exec(str).index;

Also as you’ve noticed there are only two cases, where the first letter is a vowel and where it’s not, so the final part can be done in three additional lines (based on the value of matchIndex)

See if you can think of a way to do it in a 5 line function!

1 Like

Thanks! Will try it out. In between projects but will see about getting to it this week.

I got it sooner than I thought. It’s about an additional 3 lines of code on top of what you provided. I wasn’t aware of the .exec() method. Very handy. I sometimes stick with suggested ways of solving as I’m still getting the hang of JS. Thanks for assisting. Appreciate it.

function translatePigLatin(str) {
var vowels = new RegExp("[aeiou]");
var matchIndex = vowels.exec(str).index;
if(str.indexOf(str[0]) === matchIndex) {
return str + “way”;
} else return str.substr(matchIndex, str.length) + str.substr(0,matchIndex) + “ay”;
}

translatePigLatin(“california”);

1 Like