function translatePigLatin(str) {
var regexp=/[a,e,i,o,u]/gi;
var answer='';
var a=str.split('');
var matches=str.match(regexp);
if(str[0]==matches){
answer=str+'way';
}
return answer;
}
/* if(str[0].match(regexp)){
answer=str+'way';
}
return answer;
}*/
translatePigLatin("paragraphs");
this is my code the second if statement works. i want to know why is my first if statement not working.
var matches = str.match(regexp) which is [a,a,a]
str[0]==matches eg str[0]=p … matches is a array
str[0]===matches[0] false as str is p matches[0] is a
str[1] ===matches[0] true as str[1] is a
as there are only…a… in paragraphs … for words containing eiou it will pick them up. but your are using a if statement to check just the first letter of the string against a array. you need to use a loop to check each letter of the string against each part of the array eg check string[i] === matches[i] or use filter()