Pig latin help- function to check if char is vowel

Tell us what’s happening:

My solution only works for words that start with a vowel. I tried debugging and I believe my isVowel function is always returning true, and don’t know why. Does anybody have any hint as to why that’s wrong? I can’t figure it out.

I also tried putting “a”||“e”||“i”||“o”||“u” in parenthesis, and it doesn’t work neither as the function only returns true when the word starts with a. I don’t know why that happens either…

Thanks in advance everybody!

Your code so far


function translatePigLatin(str) {
let arr = str.split("")
let j = arr.length

//Checks if a character is a vowel or not. I think my problem is here, if I am not mistaken it always returns true.
function isVowel(character){
    if(character == "a"||"e"||"i"||"o"||"u")
    {
      return true
    }
    else {
      return false
    }
}

//For each element of the array (char in the string), if it is a vowel, and it's the first element, returns the string + way. If its not a consonant, pushes it into the array. If it is a vowel and is not the first one, I splice the array to get only the part of the word after the cluster of consonants + the consonants + ay.
for(let i = 0; i < j; i++){

  if(isVowel(arr[i])){
    if(i == 0){
    str = arr.join("") + "way"
    return(str)
    }

    if(i > 0){
      let finalArr = arr.slice(i)
      finalArr = finalArr.join("")
      return(finalArr + "ay")
    }

    else {
      arr.push(arr[i])
    }
  }



  
}

}

translatePigLatin("california");

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Challenge: Pig Latin

Link to the challenge:

If statements don’t work like that. To test multiple conditions you need to use

if (cond1 || cond2 || cond3 || cond4 || cond5) {
  ...
}

Each condition needs to be logically independent. In this case, "e" is not a independent logical condition.

1 Like

Thank you! I tried this:

function isVowel(character){
if(character == “a” ||character == “e” ||character == “i” ||character == “o” ||character == “u”){
return true
}
else{
return false
}
}

and made some tweaks in the code and it is working now!

BTW, I know that’s not an elegant way to write the isVowel function at all, and I could do the same with an array of the vowels and indexOf, or something like that, but I wanted to know why it wasn’t working.

1 Like

You get the code working first and you refactor and improve after that : ) Nice work getting it to run!

1 Like