Pig Latin Simple Problem

Pig Latin Simple Problem
0

#1

why will this not return" eightway".
Can’t seem to see much wrong.


function translatePigLatin(str) {
  var vowel = /[aioue]/;
  var suffix = "way";
  if(str.charAt(0)==vowel){
    var str2 = str+suffix;
    console.log(suffix);
    return str2;
  }
 
}
 
translatePigLatin("eight");

#2

The following if statement is not comparing what you think. You assigned a regular expression literal to vowel. When you compare the first letter of str to a regular expression literal, it is going to evaluate to false, so your return str2 will never happen.

  if(str.charAt(0)==vowel){

Check out the RegExp.prototype.test function where you could use vowel to test if str.charAt(0) is a vowel.


#3

Thanks!This seems to work,could I use the match method as well as the test one.

function translatePigLatin(str) {
  var vowel = /[aioue.]/;
  var suffix = "way";
  if(vowel.test(str)){
    var str2 = str+suffix;
    console.log(suffix);
    return str2;
  }
 
}
 
translatePigLatin("eight");



#4

match is another function that works with regular expressions. It is just different in how in works.