Intermediate Algorithm Scripting: Pig Latin-works in console log but not in return

Intermediate Algorithm Scripting: Pig Latin-works in console log but not in return
0

#1

I’ve built my code and when I use console log everything works and seems to pass all the tests, but when I change the console logs to return nothing works at all. I’m lost, I’m not sure what I’m doing wrong. I’m sure it’s a simple newby problem. I’d appreciate any guidance anyone can give me. Thank you. Here’s my code.

function translatePigLatin(str) {
  let vowel = ['a', 'e', 'i', 'o', 'u'];
  return vowel.filter(function(e) {
    if (str.charAt(0) === e) {
      return str.concat('way');
    } else{
        return str.replace(/(\w+?)([aeiou]\w+)/i, '$2$1').concat('ay');
    }
  })
}

translatePigLatin("consonant");

#2

For the test case of translatePigLatin(“california”), your function returns an array which looks like [ ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ ].

You seem to not understand what the filter method returns. It returns an array of elements that pass the test implemented by the provided function. The filter callback function should return a true or false value which indicates whether or not to keep the element in the final array returned. Since your callback function always returns a non-blank string value, and non blank strings are “truthy”, then every letter in the vowel array is kept and then ultimately returned in a new array by your function.

I think you need to rethink your logic for this challenge. What should you be iterating over (str or vowels)?

Here is an example which uses the filter method to return a given string with all instances of the letter ‘a’ removed.

function removeLetterA(str) {
  const strArr = str.split(''); // splits str into an array of letters.
  const noA = strArr.filter(function(letter) {
    return letter !== 'a' && letter !== 'A'; // evaluates to true if letter is not 'a' or 'A' and keeps in final array returned (noA).
  });
  return noA.join(''); // join the array back into a string.
}

console.log(removeLetterA("california"));  // displays 'cliforni'