Whale Talk project

I am having trouble capturing the first “e” letter in my array.
My if loops are not doubling the first “e” in my input.
Can anyone tell me what I might be missing? Why isn’t my code doubling the first ‘e’

Here is my code:

let input = "elephant day i am shuffling and hustling";
let vowels = ["a", "e", "i", "o", "u"];
let resultArray = [];
for (let i = 0; i < input.length; i++) {
  //console.log([i]);
  if (input[i] === "e") {
   resultArray.push(input[i]);
  }
   if (input[i] === "u") {
    resultArray.push(input[i]);

      for (let j = 0; j < vowels.length; j++) {
        //console.log([j]);
        if (input[i] === vowels[j]) {
        //   console.log(input[i]);
         resultArray.push(input[i]);
          //console.log(resultArray);
        }
      }
}
}

console.log(resultArray);  //Prints: [ 'e', 'e', 'u', 'u', 'u', 'u']

Please read the guide about how to format your code.
That increases your chances of getting answers.

2 Likes

The second last closing curly brace should be moved just above the for loop.

if (input[i] === "u") {
    resultArray.push(input[i]);
} // <----- HERE

for (let j = 0; j < vowels.length; j++) {

In your code, the for loop is nested within the body of the second if statement. You don’t want that. The for loop should be outside both the if statements. The first two if statements push an extra "e" or "u" to the resultArray. The for loop pushes the character if it is a vowel.

1 Like

thank you.
(■■■■ curly braces :slight_smile: )