Pig Latin help please

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"));
strToArr.includes('a'||'e'||'i'||'o'||'u')

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:

console.log(strToArr.includes('a'||'e'||'i'||'o'||'u')) //prints false
console.log('a'||'e'||'i'||'o'||'u') //prints 'a'

with a testcase of california:

console.log(strToArr.includes('a'||'e'||'i'||'o'||'u')) //prints true
console.log('a'||'e'||'i'||'o'||'u') //prints 'a'

thank you I’ll try something else :slight_smile:

thank you. I kept messing up the regex even though it really was that simple. they are the bane of my existence

let regex = /[aeiou]/
  if( regex.test(str) === false)

this got me to pass all tests. thanks for the help

1 Like

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.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.