Hey guys, here’s my code for the Pig Latin challenge.
function translatePigLatin(str) {
var vowels = ["a", "e", "i", "o", "u", "y"];
var strArr = str.split("");
var tempChar = "";
var tempChar2 = "";
if (vowels.indexOf(strArr[0]) !== -1) {
// First char is a vowel
strArr.push("way");
}
else if (vowels.indexOf(strArr[0] === -1) && vowels.indexOf(strArr[1] === -1)) {
// First and second chars are consonants
console.log("strArr: " + strArr);
console.log("strArr[0]: " + strArr[0] + " = " + vowels.indexOf(strArr[0]));
console.log("strArr[1]: " + strArr[1] + " = " + vowels.indexOf(strArr[1]));
tempChar = strArr.shift();
tempChar2 = strArr.shift();
strArr.push(tempChar + tempChar2 + "ay");
}
else if (vowels.indexOf(strArr[0] === -1) && vowels.indexOf(strArr[1] !== -1)) {
// Only first char is a consonant
tempChar = strArr.shift();
strArr.push(tempChar + "ay");
}
return strArr.join("");
}
translatePigLatin("california");
I don’t understand why the two tests “California” and “Paragraphs” are passing my second “if” condition. It should only pass if the first two characters are not found in the vowel array. The console clearly shows California’s “a” character as being found (value of 0) yet it still passes my “must be equal to -1” condition. What am I missing? Slowly losing my mind here.