if someone else could take a look at this and tell me what I am doing wrong? for some reason glove and eight are both failing and I cant figure out why… I could look at the solutions in get a hint but I am trying to avoid that.
here is my code so far
function translatePigLatin(str) {
let strToArr = str.split('');// turn string into array by letter
if( strToArr.includes('a'||'e'||'i'||'o'||'u') === false){
return str.concat('ay');
} //should detect no vowels in str
if(strToArr[0] == 'a' || strToArr[0] == 'e' ||strToArr[0] == 'i' ||strToArr[0] == 'o' ||strToArr[0] == 'u'){
return strToArr.join('').concat('way');
} //should detect if word already starts with a vowel
while(strToArr[0] != 'a' && strToArr[0] != 'e' && strToArr[0] !='i' && strToArr[0] !='o' && strToArr[0] !='u'){
strToArr.push(strToArr.shift())
} //this detects if does arr does not start with vowel and makes it start with first vowel in word
return strToArr.join('').concat('ay');
}
console.log(translatePigLatin("glove"));
This is not how includes works. All this does is check if the array contains the string a. Your function will pass all test if you use another way that works for your if statement. I don’t typically use regex, but in this case a very simple regex plus the test method will work very well.
Hi, I invite you to really think about strToArr.includes('a'||'e'||'i'||'o'||'u').
You can do console.log() to see whats going on better as well, with a testcase of gloves you get:
It’s all about use, the more you use it the more you understand it, but I actively avoid regex so my skill in it is very poor. RegEx is a super powerful tool that is great to know, and I don’t recommend neglecting to learn it properly.