Whale Talk Input String Issue - Suggestion Please

Exercise LINK: Whale Talk

After tinkering around a bit on this project and trying some different ideas, I ended up with the code below. It works, with one caveat. After trying different phrases I realized the “whale talk” output won’t include the first letter of the phrase in the input string when it is a capitalized vowel.

let input = "An unruly and excessive ant attack."

const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];

for (let i = 0; i < input.length; i++) {
  //console.log([i]);
  for (let v = 0; v < vowels.length; v++) {
    if (input[i] === vowels[v]);
  }
if (input[i] === 'e' || input[i] === 'u') {
  resultArray.push(input[i], input[i]);
  } else if (input[i] === 'a' || input[i] === 'i' || input[i] === 'o') {
  resultArray.push(input[i]);
  }
}
resultArray = resultArray.join('');
console.log(resultArray.toUpperCase());

This outputs: UUUUAEEEEIEEAAA (missing the first A)

Is there a simple way to account for this and correct it without repeating this section of if/else if code using capital vowels instead??

if (input[i] === 'e' || input[i] === 'u') {
  resultArray.push(input[i], input[i]);
  } else if (input[i] === 'a' || input[i] === 'i' || input[i] === 'o') {
  resultArray.push(input[i]);
  }

Any ideas would be appreciated! Thanks

If you don’t want to check for capitals, could you not convert the string to lower case?

Bingo!!! Face palm. Sometimes the obvious solution is right in front of you.
I just added this on line 2 and problem solved:

input = input.toLowerCase();

Thanks neutrino

1 Like