Help Me - I'm Stuck With Adding Letters to An Array

I’ve been stuck on this same step for a while now and I can’t figure out what I’m doing wrong …

I’m trying to take the following code:

const input = ‘turpentine and turtles’;
const vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’];
let resultsArray = ;

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

And this does what the question asks and compares the input letter to every letter in the vowels array resulting in: u
e
i
e
a
u
e

My issue arises when I try to “push” this to the resultsArray … The following code:

const input = ‘turpentine and turtles’;
const vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’];
let resultsArray = ;

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

Yields this:

Output-only Terminal
Output:
[ ‘u’ ]
[ ‘u’, ‘e’ ]
[ ‘u’, ‘e’, ‘i’ ]
[ ‘u’, ‘e’, ‘i’, ‘e’ ]
[ ‘u’, ‘e’, ‘i’, ‘e’, ‘a’ ]
[ ‘u’, ‘e’, ‘i’, ‘e’, ‘a’, ‘u’ ]
[
‘u’, ‘e’, ‘i’,
‘e’, ‘a’, ‘u’,
‘e’
]

What am I doing wrong? I’m so confused.

To preserve code formatting in forum posts, see: [How to] Format code in posts

You haven’t linked to the exercise/project. What is the link of the exercise/project? What is the exact wording of the step you are stuck on? What are the exact instructions mentioned in the step?

In the first snippet of code that you have posted, you are logging the individual letters that meet the condition via the statement console.log(vowels[j])

In the second snippet, you are pushing the matched letters to resultsArray and logging the whole array.

What task are you trying to accomplish?

Do you want to log the array only once and not repeatedly? In that case, you should move the statement console.log(resultsArray) outside and below the loops.

If you want something else to happen, then elaborate on what output you were hoping to see.

1 Like

Let’s format the code at issue. With some minor corrections:

const input = 'turpentine and turtles';
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultsArray = [];

for (let i = 0; i < input.length; i++) {
    // { this extra block is confusing
    for (let j = 0; j < vowels.length; j++) {
        if (input[i] === vowels[j]) {
            resultsArray.push(vowels[j]);
            // you're printing here, which is also confusing
            // console.log(resultsArray)
        }
    }
    // }  other end of superfluous block
}
// let's show the array here
console.log(resultsArray)

Result:

[
  'u', 'e', 'i',
  'e', 'a', 'u',
  'e'
]

Not sure if that’s what you’re after. If you look carefully, it’s identical to your last entry on the original code.

I you wanted to dump that array out one line at a time, you’ll want another loop. Or, array has a forEach if you’ve gotten that far. (The link, coincidentally, shows exactly how to do that.)

I’d add that there is an array function that could get rid of you checking loop: includes.

2 Likes

BRO THANK YOU!!

Definitely learned something here … that if I am printing my results I need to be outside of the function … as you can tell, this is all very new to me but this was SO HELPFUL