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: